Skip to content

Parser API

The parser module provides utilities for parsing Gemtext content.

Overview

Gemtext is the text format used by Gemini. It supports:

  • Three levels of headings (#, ##, ###)
  • Links (=> prefix)
  • Unordered lists (* prefix)
  • Blockquotes (> prefix)
  • Preformatted blocks (``` fences)
  • Plain text

API Reference

parser

Gemtext parser for the Astronomo Gemini browser.

This module implements a parser for the Gemtext markup format as specified at: https://geminiprotocol.net/docs/gemtext.gmi

LineType

Bases: StrEnum

Enum representing the different types of Gemtext lines.

GemtextLine dataclass

GemtextLine(line_type: LineType, content: str, raw: str)

Represents a parsed line of Gemtext.

parse_gemtext

parse_gemtext(content: str) -> list[GemtextLine]

Convenience function to parse Gemtext content.

Parameters:

Name Type Description Default
content str

The Gemtext content as a string.

required

Returns:

Type Description
list[GemtextLine]

A list of GemtextLine objects representing the parsed content.

Source code in src/astronomo/parser.py
def parse_gemtext(content: str) -> list[GemtextLine]:
    """Convenience function to parse Gemtext content.

    Args:
        content: The Gemtext content as a string.

    Returns:
        A list of GemtextLine objects representing the parsed content.
    """
    parser = GemtextParser()
    return parser.parse(content)

Example Usage

from astronomo.parser import parse_gemtext, LineType

gemtext = """# Welcome
=> gemini://example.com/ Example Site
Some plain text.
"""

lines = parse_gemtext(gemtext)
for line in lines:
    if line.line_type == LineType.LINK:
        print(f"Link: {line.url} -> {line.text}")
    elif line.line_type == LineType.HEADING:
        print(f"H{line.level}: {line.text}")