gemmula-1.2.0: a tiny gemtext parser
Copyright(c) 2024 Sena
LicenseLGPL-3.0-or-later
Maintainercontact@sena.pink
Stabilitystable
Portabilityportable
Safe HaskellSafe-Inferred
LanguageHaskell2010

Text.Gemini

Description

Parse gemtext (text/gemini) documents from and back into Text.

See the Gemini hypertext format specification at https://geminiprotocol.net/docs/gemtext-specification.gmi.

Synopsis

Gemtext types

type GemDocument = [GemItem] Source #

A gemtext document, in the form of a list of GemItems

data GemItem Source #

A gemtext item

Constructors

GemText !Text

A regular gemtext line -- GemText <Text>

GemLink !Text !(Maybe Text)

A gemtext link -- GemLink <Link> [Optional Description]

GemHeading !Int !Text

A gemtext heading of 3 levels max -- GemHeading <Level> <Text>

GemList ![Text]

An unordered gemtext list -- GemList <Lines>

Gemtext specification does not endorse grouping list items this way. This approach is purely for practical reasons, such as ease of conversion to unordered HTML lists.

GemQuote !Text

A gemtext quote -- GemQuote <Text>

GemPre ![Text] !(Maybe Text)

A preformatted gemtext block -- GemPre <Lines> [Optional Alt Text]

Instances

Instances details
Show GemItem Source # 
Instance details

Defined in Text.Gemini

Eq GemItem Source # 
Instance details

Defined in Text.Gemini

Methods

(==) :: GemItem -> GemItem -> Bool #

(/=) :: GemItem -> GemItem -> Bool #

Decoding from text

decode :: Text -> GemDocument Source #

Parse a gemtext block as a GemDocument.

The input Text must use either CRLF or LF.

decodeLine :: Text -> GemItem Source #

Parse a single gemtext line as GemItem.

There should not be any line breaks in the input; if present, they are treated as spaces. Preformatted text blocks are regarded as GemTexts as they are strictly multiline.

Encoding into text

encode :: GemDocument -> Text Source #

Encode a parsed GemDocument into a gemtext block.

The output Text uses CRLF line breaks and ends with a newline.

Empty lists are ignored completely if present. See the encodeItem function for additional quirks.

encodeItem :: GemItem -> Text Source #

Encode a single parsed GemItem into gemtext.

The output Text does not end with an additional newline, and might be multiple lines, in which case it uses CRLF line breaks.

Prefixes are escaped as necessary before encoding and the spaces in GemLinks are replaced with %20 to normalize them. Line breaks inside the body of an item are also replaced with a space.

Utility functions

documentTitle :: GemDocument -> Maybe Text Source #

Get the title of the GemDocument, which is the very first GemHeading in the document, regardless of the level of the heading.