[[tags: egg gmi]] == gmi [[toc:]] === Description This egg can be used to read and manipulate 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))) This procedure can be used to read Gemtext. {{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. *strict-gemtext-headers* 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 {{`(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}}, ... Next, each set of procedures of a Gemtext feature is described. (gmi:text? obj) (gmi:text text) 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? obj) (gmi:header level title) (gmi:header:level header) (gmi:header:text header) {{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? obj) (gmi:link uri text) (gmi:link:uri link) (gmi:link:text link) {{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? obj) (gmi:list* items) (gmi:list . items) (gmi:list:items list) {{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? obj) (gmi:blockquote text) (gmi:blockquote:text blockquote) {{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? obj) (gmi:code* lines) (gmi:code alt-text . lines) (gmi:code:text code) (gmi:code:lines code) {{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}}. === 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: '((header 1 "Factorial") "" "There are two steps to compute the factorial of a number:" "" (list "Compute the list of integers from 1 up to the number" "Multiply all the integers of the list") "" (link "https://en.wikipedia.org/wiki/Factorial" "") (link "gemini://gemi.dev/cgi-bin/wp.cgi/view?Factorial" "Factorial (Gemipedia)") "" (header 2 "Haskell code") "" "Here's the code in Haskell:" "" (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 === Version History ==== 0.0.1 (2022/10/28) * Initial release. ==== 0.0.2 (2022/10/28) * Make CHICKEN imports explicit. ==== 0.0.3 (2023/01/30) * Remove dead code.