chickadee » gmi

gmi

Description

This egg can be used to read, manipulate, and write Gemtext, the Gemini protocol's "default" text format.

Author

siiky

Repository

https://git.sr.ht/~siiky/gmi.scm

Requirements

This egg has no dependencies apart from CHICKEN itself.

API

Reading

(gmi:read #!optional (port (current-input-port)))procedure

This procedure can be used to read a Gemtext document. port determines where to read from and is optional (defaults to (current-input-port)).

The returned value is an object that may be accessed and manipulated with the gmi:* procedures documented below.

If the input file ends in the middle of a code block, everything up to, but not including, the code block will be returned. Additionally a string error message will be returned as the second value.

*strict-gemtext-headers*parameter

Headers in Gemtext go up to level 3. This parameter controls whether gmi:read should read headers strictly in "compliance mode" (the default) or more leniently. In "compliance mode", headers with level above 3 are read as normal text rather than as headers; in the more lenient mode, headers with level above 3 are read as headers with the corresponding level (possibly greater than 3).

Manipulation

Each "feature" of the Gemtext format has a corresponding set of predicates, constructor(s), and accessors, for convenience -- no setters/updaters have been defined because constructors are all so simple and of so few arguments.

However, simple Scheme types are used to represent all of a Gemtext document and its features -- for example, (gmi:header level title) is defined as `(,gmi:tag/header ,level ,title). This should prove useful in case the procedures defined and exported from this egg don't suffice for your use case.

Note that constructors don't make any type checks on a feature's fields, but predicates do!

The used terminology will be obj for any Scheme object, and a descriptive name for parameters and Gemtext features: text, header, link, list, blockquote, code. Fields of these feature will also have a descriptive name -- e.g. level, title, uri, items, ...

gmi:tag/headerconstant
gmi:tag/listconstant
gmi:tag/blockquoteconstant
gmi:tag/codeconstant

The tag constant of each Gemtext feature, except for plain text -- more next.

gmi:text? objprocedure
gmi:text textprocedure

This represents "normal text". No encapsulation is used here, text is represented as the string of that text itself. gmi:text? is an alias of string? and gmi:text is the unary identity function.

gmi:header? objprocedure
gmi:header level titleprocedure
gmi:header:level headerprocedure
gmi:header:text headerprocedure

level is a fixnum and title a string.

For a header created by gmi:read, if *strict-gemtext-headers* is truthy (the default), then (<= 1 level 3); otherwise (<= 1 level).

gmi:link? objprocedure
gmi:link:uri linkprocedure
gmi:link:text linkprocedure

uri and text are strings. Similarly to code blocks, the text field of a link with no alt-text is an empty string.

gmi:list? objprocedure
gmi:list* itemsprocedure
gmi:list #!rest itemsprocedure
gmi:list:items listprocedure

items as returned by gmi:list:items is a Scheme list of all the items (each of them a string) of the (Gemtext) list.

gmi:blockquote? objprocedure
gmi:blockquote textprocedure
gmi:blockquote:text blockquoteprocedure

text is a string.

Each blockquote object represents a single textual line. Several textual (blockquote) lines result in several blockquote objects even if not separated by any blank lines.

gmi:code? objprocedure
gmi:code* linesprocedure
gmi:code alt-text #!rest linesprocedure
gmi:code:text codeprocedure
gmi:code:lines codeprocedure

alt-text is a string and lines is a Scheme list of strings. Similarly to links, the alt-text field of a code block with no alt-text is an empty string.

The first element of the lines argument given to gmi:code* must be the alt-text, not the first line of code! The first line of code should be the second element of lines.

Writing

(gmi:write gmi #!optional (port (current-output-port)))procedure
(gmi:write1 elem #!optional (port (current-output-port)))procedure

gmi:write may be used to write a Gemtext document, represented by gmi.

gmi:write1 may be used to write a Gemtext element, elem, such as a header, a list, &c. This procedure takes care of adding the necessary newlines, no extra work required.

port determines where to write to and is optional (defaults to (current-output-port)).

Examples

If you save the following Gemtext document:

# Factorial

There are two steps to compute the factorial of a number:

* Compute the list of integers from 1 up to the number
* Multiply all the integers of the list

=> https://en.wikipedia.org/wiki/Factorial
=> gemini://gemi.dev/cgi-bin/wp.cgi/view?Factorial Factorial (Gemipedia)

## Haskell code

Here's the code in Haskell:

```hs
fact n = prod [1..n]
```

And run this code with that file as stdin:

(import gmi)
(write (gmi:read))

You get the following output:

'((gmi:header 1 "Factorial")
  ""
  "There are two steps to compute the factorial of a number:"
  ""
  (gmi:list "Compute the list of integers from 1 up to the number"
            "Multiply all the integers of the list")
  ""
  (gmi:link "https://en.wikipedia.org/wiki/Factorial" "")
  (gmi:link "gemini://gemi.dev/cgi-bin/wp.cgi/view?Factorial" "Factorial (Gemipedia)")
  ""
  (gmi:header 2 "Haskell code")
  ""
  "Here's the code in Haskell:"
  ""
  (gmi:code "hs"
            "fact n = prod [1..n]"))

License

 This is free and unencumbered software released into the public domain.
 
 Anyone is free to copy, modify, publish, use, compile, sell, or
 distribute this software, either in source code form or as a compiled
 binary, for any purpose, commercial or non-commercial, and by any
 means.
 
 In jurisdictions that recognize copyright laws, the author or authors
 of this software dedicate any and all copyright interest in the
 software to the public domain. We make this dedication for the benefit
 of the public at large and to the detriment of our heirs and
 successors. We intend this dedication to be an overt act of
 relinquishment in perpetuity of all present and future rights to this
 software under copyright law.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.
 
 For more information, please refer to <http://unlicense.org>

Version History

0.0.1 (2022/10/28)

0.0.2 (2022/10/28)

0.0.3 (2023/01/30)

0.0.4 (2023/02/03)

0.0.5 (2023/02/03)

0.0.6 (2023/02/06)

0.0.7 (2023/02/08)

Contents »