; Feeds' render: a feed tree's events written as one Atom 1.0 document.
; This is a library of definitions, each named `feed-...`, which a host
; links with its own program (alchemy's `compile_sources`); its entry
; point is `feed-render`, and nothing else here is the host's to call.
;
; The tree is the one the reader builds by default, the Atom-shaped feed
; it makes of an Atom 1.0 or 0.3 document and of an RSS 0.90 to 2.0 one,
; or one the embedding (`alchemy/embed.alc`) made of a plain tree:
;
; feed format and version (read, not written), entries, id, title,
; subtitle, rights, updated, authors, contributors, categories,
; links, generator, icon, logo
; entry id, title, updated, published, authors, contributors,
; categories, links, content, rights, summary, source
; source the feed's members but entries
; person name, uri, email (an author or a contributor)
; text type and value (a title, subtitle, rights or summary)
; content type, src and value
; generator uri, version and value
; category term, scheme and label; link href, rel, type, hreflang,
; title and length
;
; Each member is written as the element Atom names for it: a string as
; an element's text, a list as one element for each item (`authors` as
; `author` elements, `entries` as `entry` elements), a category and a
; link as an element whose members are its attributes, and a text
; construct, a content and a generator as an element whose `value` is
; its text and whose other members are its attributes. The document
; begins with an XML declaration and the `feed` element in Atom 1.0's
; namespace, so the reader reads it back as Atom 1.0 whatever version the
; tree says; an element with no children is written on one line, every
; other element's children one to a line, two spaces deeper. A text
; construct or a content of type `xhtml` is written as `html`, its markup
; as escaped text: the reader keeps an xhtml value's markup as text, its
; own text unescaped, so writing it back as markup could make a document
; that is not XML, and an Atom reader shows escaped html as the same
; markup. Character data has `&`, `<` and `>` written as `&`, `<`
; and `>`, and an attribute's value, always double-quoted, has `&`,
; `<` and `"` written as `&`, `<` and `"`; a carriage return
; in either, and a tab or a line feed in an attribute, are written as
; ` `, `	` and ` `, since a reader normalises a bare one away.
; A number or a boolean where Atom has text is written as its text (a
; link's `length` is a number), and a member whose value is null is not
; written.
;
; Atom puts a feed's own elements before its entries, and every reader
; and the embedding build a feed so, `entries` its last member (admin
; ADR-33): the feed's members are written in the order the tree holds
; them, and each entry as it comes, captured on its own under
; max_record_bytes, so nothing is held. A member of the feed after its
; entries, which no reader builds, is refused part way with
; TARGET_VALUE_UNREPRESENTABLE, since the elements that go before the
; entries are written by then. Atom gives the children of an entry, a source and a
; person no order, and the reader takes any, so their members are written
; in the tree's order. An entry's `source`, which the reader makes of an
; RSS item's source, is written as Atom's `source` element, which the
; reader does not read.
;
; Atom requires an id, a title and an updated of a feed and of each
; entry, and an RSS channel or item need not give the reader any of them.
; One the tree does not have, or has as null, is written by a convention:
; a feed's id as `tag:tabnas.dev,2026:feed-render`, and an entry's as that
; id, a slash and the entry's position among the feed's entries from 0,
; so no two of them are alike; a title as an empty title of type text;
; an updated as 1970-01-01T00:00:00Z, the date the embedding gives a plain
; tree. Each is written after the other members of its feed or entry, and
; reads back as written.
;
; Atom's updated and published are dates in RFC 3339's form, and an RSS
; date is in RFC 822's. A date in RFC 3339's form is written as it is,
; but for its t and z, which RFC 3339 allows in lower case and Atom asks
; for in upper. A date in RFC 822's form is written as the same instant
; in RFC 3339's: the day, the month and the year, a two-digit year before
; 50 in the 2000s and from 50 in the 1900s, as RFC 2822 reads one; the
; time with its seconds, 00 where it has none; and the zone as Z for UT,
; GMT and the military Z, or as the offset it names, EST as -05:00 and
; +0100 as +01:00. Its names may be in any case, as RFC 822's may, and a
; tab or a line break in it is a space. A date in neither form, one with
; a comment or with a military zone but Z among them, is written as
; 1970-01-01T00:00:00Z, followed by a category that keeps its text: the
; member's name its term, the text its label, and its scheme
; `tag:tabnas.dev,2026:feed-render/date`, which reads back as that
; category. A date with no text is written as a missing one is.
;
; Atom requires an author of a feed unless every entry has one. A feed
; whose author the tree does not give, as none, null or an empty list, is
; written with the render's after its other members and before its first
; entry, since what follows cannot be seen: named by the feed's title
; where that is text and not empty and `unknown` otherwise, with the
; render's id as its uri.
;
; A tree that is not a feed fails with TARGET_VALUE_UNREPRESENTABLE,
; naming what was met: a root that is not an object, a member a feed, an
; entry, a source or one of their parts does not have, or a value of the
; wrong kind. A character XML 1.0 cannot carry (a control below U+0020
; but the tab, the line feed and the carriage return, U+FFFE or U+FFFF),
; which the reader builds only from a character reference no well-formed
; document holds, is written as U+FFFD, the replacement character.
; Events no tree has fail with PROTOCOL_ORDER_ERROR, a member of the feed
; met twice among them.
;
; The render is three stages. The check (`feed-check`) reads the tree's
; events and hands them on: it refuses what is not a feed's, drops a
; member whose value is null and the `format` and `version` it does not
; write, and adds the members the conventions above write, the feed's
; before its entries. The router captures each of the feed's members and
; each entry, one at a time, and hands each on when it ends. The writer
; (`feed-write-step`) writes each as it comes, the document's start
; before the first.
;
; The check's state is a stack of markers, one for each container it is
; inside:
;
; [:feed slots done] the feed; `slots` says of each member a feed
; can have whether it is :absent, met with a
; null value or none yet (:null) or :present,
; and `done` whether the feed's conventions
; are out, as they are once its entries begin
; [:entry present pos] an entry; `present` says whether its id,
; title and updated are, and `pos` is its
; position as a vector of decimal digits
; (alchemy has no arithmetic)
; [:obj ctx] a source or a person
; [:due member name out] a member's value due, its key `name` held
; until the value begins, to be handed on as
; `out`; `member` says what it is written as
; [:cons sort el] a text construct, a content or a generator
; (`sort`), written as the element `el`
; [:cdue sort el name] the same, a member's value due
; [:items sort el pos] a list of persons, entries, categories or
; links; `pos` the next entry's position
; [:attrs el] a category or a link
; [:adue el name] the same, an attribute's value due
;
; Once the feed is closed the state is `[:done]`, and nothing may follow
; it. The writer's state is `[:start]` before the first value, `[:open
; author title]` after it, and `[:entries]` once an entry is written:
; `author` says whether the feed's author is written, and `title` is the
; text of its title. Nothing grows with a document's width or length but
; an entry's position, by a digit each time the count of entries grows
; tenfold.
; The characters XML 1.0 can carry, as code point ranges.
def feed-xml-chars [[9 10] [13 13] [32 55295] [57344 65533] [65536 1114111]]
def feed-fail [what]
fail :unrepresentable (string-join "" ["the tree is not a feed: " what])
; A value's kind, in words.
def feed-kind [value]
match (kind value)
case :null "null"
case :boolean "a boolean"
case :number "a number"
case :string "a string"
case _ "a container"
; A string with every character XML 1.0 cannot carry written as U+FFFD,
; the replacement character: the controls below U+0020 but the tab, the
; line feed and the carriage return, and U+FFFE and U+FFFF. XML has no
; spelling for one, not even a character reference, and the reader builds
; one only from a document that is not well formed (``).
def feed-scrub [value]
replace-text "\u0000" "\ufffd"
replace-text "\u0001" "\ufffd"
replace-text "\u0002" "\ufffd"
replace-text "\u0003" "\ufffd"
replace-text "\u0004" "\ufffd"
replace-text "\u0005" "\ufffd"
replace-text "\u0006" "\ufffd"
replace-text "\u0007" "\ufffd"
replace-text "\u0008" "\ufffd"
replace-text "\u000b" "\ufffd"
replace-text "\u000c" "\ufffd"
replace-text "\u000e" "\ufffd"
replace-text "\u000f" "\ufffd"
replace-text "\u0010" "\ufffd"
replace-text "\u0011" "\ufffd"
replace-text "\u0012" "\ufffd"
replace-text "\u0013" "\ufffd"
replace-text "\u0014" "\ufffd"
replace-text "\u0015" "\ufffd"
replace-text "\u0016" "\ufffd"
replace-text "\u0017" "\ufffd"
replace-text "\u0018" "\ufffd"
replace-text "\u0019" "\ufffd"
replace-text "\u001a" "\ufffd"
replace-text "\u001b" "\ufffd"
replace-text "\u001c" "\ufffd"
replace-text "\u001d" "\ufffd"
replace-text "\u001e" "\ufffd"
replace-text "\u001f" "\ufffd"
replace-text "\ufffe" "\ufffd"
replace-text "\uffff" "\ufffd" value
; A scalar as text: a string, its characters XML 1.0 cannot carry as
; U+FFFD; a number by its lexeme or as JSON writes it (`Infinity`,
; `-Infinity` or `NaN` where it is not finite); a boolean by its name.
def feed-text [value]
match (kind value)
case :string
if (chars-within feed-xml-chars value)
value
feed-scrub value
case :number
match (number-class value)
case :finite (scalar-text csv-options value)
case :infinity "Infinity"
case :negative-infinity "-Infinity"
case :nan "NaN"
case :boolean (scalar-text csv-options value)
; Character data.
def feed-chars [value]
replace-text "\r" " "
replace-text ">" ">"
replace-text "<" "<"
replace-text "&" "&" (feed-text value)
; An attribute's value, between double quotes.
def feed-attr [value]
replace-text "\r" " "
replace-text "\n" " "
replace-text "\t" "	"
replace-text "\"" """
replace-text "<" "<"
replace-text "&" "&" (feed-text value)
; What an object is, for the failures.
def feed-where [ctx]
match ctx
case :feed "the feed"
case :entry "an entry"
case :source "an entry's source"
case :person "a person"
; An item of a list, for the failures.
def feed-item-name [el]
match el
case "entry" "an entry"
case "author" "an author"
case "contributor" "a contributor"
case "category" "a category"
case "link" "a link"
; The members a feed, a source and an entry share.
def feed-common-member [name]
match name
case "id" [:string "id"]
case "updated" [:date "updated"]
case "title" [:text "title"]
case "rights" [:text "rights"]
case "authors" [:list :person "author"]
case "contributors" [:list :person "contributor"]
case "categories" [:list :category "category"]
case "links" [:list :link "link"]
case _ :none
; The members of a feed and of a source.
def feed-head-member [name]
match name
case "format" [:skip name]
case "version" [:skip name]
case "subtitle" [:text "subtitle"]
case "generator" [:cons :generator "generator"]
case "icon" [:string "icon"]
case "logo" [:string "logo"]
case _ (feed-common-member name)
def feed-entry-member [name]
match name
case "published" [:date "published"]
case "summary" [:text "summary"]
case "content" [:cons :content "content"]
case "source" [:source "source"]
case _ (feed-common-member name)
def feed-person-member [name]
match name
case "name" [:string "name"]
case "uri" [:string "uri"]
case "email" [:string "email"]
case _ :none
; What a member of an object is written as, or :none.
def feed-member [ctx name]
match ctx
case :person (feed-person-member name)
case :entry (feed-entry-member name)
case :source (feed-head-member name)
case :feed
match name
case "entries" [:list :entry "entry"]
case _ (feed-head-member name)
; A member's value, as the member asks for it, for the failures.
def feed-wants [member]
match member
case [:string name] (string-join "" ["its " name " is text"])
case [:date name] (string-join "" ["its " name " is text"])
case [:skip name] (string-join "" ["its " name " is text"])
case [:text name] (string-join "" ["its " name " is an object of type and value"])
case [:cons _ name] (string-join "" ["its " name " is an object"])
case [:source _] "its source is an object"
case [:list _ el] (string-join "" ["its " el "s are a list"])
; Whether a member of a text construct, a content or a generator is one
; it has: its `value`, its `type`, or another attribute.
def feed-cons-member [sort name]
match [sort name]
case [_ "value"] true
case [:text "type"] true
case [:content "type"] true
case [:content "src"] true
case [:generator "uri"] true
case [:generator "version"] true
case _ false
; The attributes of a category and of a link.
def feed-attribute [el name]
match [el name]
case ["category" "term"] true
case ["category" "scheme"] true
case ["category" "label"] true
case ["link" "href"] true
case ["link" "rel"] true
case ["link" "type"] true
case ["link" "hreflang"] true
case ["link" "title"] true
case ["link" "length"] true
case _ false
; A type as written: xhtml as html.
def feed-type [value]
match value
case "xhtml" "html"
case _ value
; The top marker, or :none before the root value.
def feed-top [s]
match (count s)
case 0 :none
case _ (top s)
def feed-mark [marker s]
push marker (pop s)
def feed-deeper [in]
string-join "" [in " "]
; A vector with its item at position i replaced by x.
def feed-put [v i x]
map
fn [j]
match (compare j i)
case :equal x
case _ (get-path (path j) v)
indices v
; ---------------------------------------------------------------------
; The conventions: what a feed or an entry Atom requires is written as
; when the tree does not have it
; ---------------------------------------------------------------------
; A feed's id, and the start of an entry's, which a slash and its
; position end.
def feed-render-id "tag:tabnas.dev,2026:feed-render"
def feed-render-updated "1970-01-01T00:00:00Z"
; The next decimal digit, and the successor of a vector of them, most
; significant first: an entry's position.
def feed-digit [d]
match d
case "0" "1"
case "1" "2"
case "2" "3"
case "3" "4"
case "4" "5"
case "5" "6"
case "6" "7"
case "7" "8"
case "8" "9"
case "9" "0"
def feed-next [ds]
let [low (filter (fn [i] (match (get-path (path i) ds) (case "9" false) (case _ true))) (indices ds))]
match (count low)
case 0 (map (fn [i] (match i (case 0 "1") (case _ "0"))) (indices (push "0" ds)))
case _
let [p (top low)]
map
fn [i]
match (compare i p)
case :less (get-path (path i) ds)
case :equal (feed-digit (get-path (path i) ds))
case _ "0"
indices ds
; The members' events, appended to the events in v, for an id, a title
; and an updated each unless has.
def feed-complete-id [has id v]
if has
v
push (scalar id) (push (key "id") v)
def feed-complete-title [has v]
if has
v
push object-end (push (scalar "") (push (key "value") (push (scalar "text") (push (key "type") (push object-start (push (key "title") v))))))
def feed-complete-updated [has v]
if has
v
push (scalar feed-render-updated) (push (key "updated") v)
; ---------------------------------------------------------------------
; Dates: Atom's updated and published are RFC 3339 date-times, and an
; RSS date is in RFC 822's form
; ---------------------------------------------------------------------
; The scheme of the category that keeps a date the render cannot read.
def feed-date-scheme "tag:tabnas.dev,2026:feed-render/date"
def feed-at [v i]
get-path (path i) v
def feed-digits [[48 57]]
; Whether s is n decimal digits.
def feed-is-digits [n s]
match (chars-within feed-digits s)
case false false
case true
match (compare (length s) n)
case :equal true
case _ false
; Whether s is two digits from lo to hi, each given as 100 more: alchemy
; has no arithmetic, and `number` reads no leading zero, so the digits
; are read with a 1 before them.
def feed-two [lo hi s]
match (feed-is-digits 2 s)
case false false
case true
let [n (number (string-join "" ["1" s]))]
match [(compare n lo) (compare n hi)]
case [:less _] false
case [_ :greater] false
case _ true
; Whether p begins with the string c, which is not empty, and p without
; that first c.
def feed-begins [c p]
let [parts (split c p)]
match (feed-at parts 0)
case "" (match (compare (count parts) 1) (case :greater true) (case _ false))
case _ false
def feed-after [c p]
let [parts (split c p)]
string-join c (map (fn [i] (feed-at parts i)) (filter (fn [i] (match (compare i 0) (case :greater true) (case _ false))) (indices parts)))
; s with its ASCII letters in lower case.
def feed-lower [s]
string-join "a" (split "A" (string-join "b" (split "B" (string-join "c" (split "C" (string-join "d" (split "D" (string-join "e" (split "E" (string-join "f" (split "F" (string-join "g" (split "G" (string-join "h" (split "H" (string-join "i" (split "I" (string-join "j" (split "J" (string-join "k" (split "K" (string-join "l" (split "L" (string-join "m" (split "M" (string-join "n" (split "N" (string-join "o" (split "O" (string-join "p" (split "P" (string-join "q" (split "Q" (string-join "r" (split "R" (string-join "s" (split "S" (string-join "t" (split "T" (string-join "u" (split "U" (string-join "v" (split "V" (string-join "w" (split "W" (string-join "x" (split "X" (string-join "y" (split "Y" (string-join "z" (split "Z" s)))))))))))))))))))))))))))))))))))))))))))))))))))
; s with its t and z in upper case.
def feed-upper-tz [s]
string-join "T" (split "t" (string-join "Z" (split "z" s)))
; The parts of an RFC 3339 date-time: a full date; a time, whose seconds
; may have a fraction; an offset, Z or a sign and hours and minutes.
def feed-3339-date [d]
let [p (split "-" d)]
match (count p)
case 3
match [(feed-is-digits 4 (feed-at p 0)) (feed-two 101 112 (feed-at p 1)) (feed-two 101 131 (feed-at p 2))]
case [true true true] true
case _ false
case _ false
def feed-3339-second [sec]
let [p (split "." sec)]
match (count p)
case 1 (feed-two 100 160 sec)
case 2
match [(feed-two 100 160 (feed-at p 0)) (chars-within feed-digits (feed-at p 1)) (compare (length (feed-at p 1)) 0)]
case [true true :greater] true
case _ false
case _ false
def feed-3339-time [t]
let [p (split ":" t)]
match (count p)
case 3
match [(feed-two 100 123 (feed-at p 0)) (feed-two 100 159 (feed-at p 1)) (feed-3339-second (feed-at p 2))]
case [true true true] true
case _ false
case _ false
def feed-3339-offset [off]
let [p (split ":" off)]
match (count p)
case 2
match [(feed-two 100 123 (feed-at p 0)) (feed-two 100 159 (feed-at p 1))]
case [true true] true
case _ false
case _ false
def feed-3339-pair [t off]
match [(feed-3339-time t) (feed-3339-offset off)]
case [true true] true
case _ false
def feed-3339-rest [r]
let [z (split "Z" r)]
match (count z)
case 2
match (feed-at z 1)
case "" (feed-3339-time (feed-at z 0))
case _ false
case 1
let [plus (split "+" r)]
match (count plus)
case 2 (feed-3339-pair (feed-at plus 0) (feed-at plus 1))
case 1
let [minus (split "-" r)]
match (count minus)
case 2 (feed-3339-pair (feed-at minus 0) (feed-at minus 1))
case _ false
case _ false
case _ false
; Whether s is an RFC 3339 date-time with the upper-case T and Z Atom
; requires.
def feed-is-3339 [s]
let [p (split "T" s)]
match (count p)
case 2
match (feed-3339-date (feed-at p 0))
case true (feed-3339-rest (feed-at p 1))
case false false
case _ false
; RFC 822's names, in lower case: the days, the months, and the zones as
; RFC 3339 offsets (UT and GMT and the military Z as Z, the US zones by
; their hours).
def feed-822-day-name [d]
match d
case "mon" true
case "tue" true
case "wed" true
case "thu" true
case "fri" true
case "sat" true
case "sun" true
case _ false
def feed-822-month [m]
match m
case "jan" "01"
case "feb" "02"
case "mar" "03"
case "apr" "04"
case "may" "05"
case "jun" "06"
case "jul" "07"
case "aug" "08"
case "sep" "09"
case "oct" "10"
case "nov" "11"
case "dec" "12"
case _ :none
def feed-822-zone [z]
match z
case "ut" "Z"
case "gmt" "Z"
case "z" "Z"
case "est" "-05:00"
case "edt" "-04:00"
case "cst" "-06:00"
case "cdt" "-05:00"
case "mst" "-07:00"
case "mdt" "-06:00"
case "pst" "-08:00"
case "pdt" "-07:00"
case _ :none
def feed-hours ["00" "01" "02" "03" "04" "05" "06" "07" "08" "09" "10" "11" "12" "13" "14" "15" "16" "17" "18" "19" "20" "21" "22" "23"]
; A zone of a sign and four digits, hours to 23 and minutes to 59, as an
; RFC 3339 offset, or :none.
def feed-822-numeric [sign digits]
match (feed-is-digits 4 digits)
case false :none
case true
let [hs (filter (fn [h] (feed-begins h digits)) feed-hours)]
match (count hs)
case 0 :none
case _
let [mm (feed-after (feed-at hs 0) digits)]
match (feed-two 100 159 mm)
case true (string-join "" [sign (feed-at hs 0) ":" mm])
case false :none
def feed-822-offset [z]
match (feed-822-zone (feed-lower z))
case :none
match (split "+" z)
case ["" digits] (feed-822-numeric "+" digits)
case _
match (split "-" z)
case ["" digits] (feed-822-numeric "-" digits)
case _ :none
case off off
; A day of one or two digits, a year of four (RSS's) or two (read as RFC
; 2822 reads one: before 50 in the 2000s, else in the 1900s), and a time
; of hours and minutes and perhaps seconds, as RFC 3339 writes each.
def feed-822-day [d]
let [dd (match (feed-is-digits 1 d) (case true (string-join "" ["0" d])) (case false d))]
match (feed-two 101 131 dd)
case true dd
case false :none
def feed-822-year [y]
match [(feed-is-digits 4 y) (feed-is-digits 2 y)]
case [true _] y
case [_ true]
match (compare (number (string-join "" ["1" y])) 150)
case :less (string-join "" ["20" y])
case _ (string-join "" ["19" y])
case _ :none
def feed-822-time [t]
let [p (split ":" t)]
let [hms (match (count p) (case 2 (push "00" p)) (case _ p))]
match (count hms)
case 3
match [(feed-two 100 123 (feed-at hms 0)) (feed-two 100 159 (feed-at hms 1)) (feed-two 100 160 (feed-at hms 2))]
case [true true true] (string-join ":" hms)
case _ :none
case _ :none
def feed-822-fields [day mon year time zone]
let [parts [(feed-822-year year) "-" (feed-822-month (feed-lower mon)) "-" (feed-822-day day) "T" (feed-822-time time) (feed-822-offset zone)]]
match (count (filter (fn [x] (match x (case :none true) (case _ false))) parts))
case 0 (string-join "" parts)
case _ :none
; The words of a text, its tabs and line breaks read as spaces.
def feed-words [t]
filter (fn [w] (match w (case "" false) (case _ true))) (split " " (string-join " " (split "\t" (string-join " " (split "\r" (string-join " " (split "\n" t)))))))
def feed-822-words [w]
match w
case [day mon year time zone] (feed-822-fields day mon year time zone)
case _ :none
; An RSS date, RFC 822's date-time with RSS's four-digit year allowed and
; its names in any case, as RFC 822 has them, as the same instant in RFC
; 3339, or :none: a day of the week and a comma, then the day, the month,
; the year, the time and the zone. One with a comment, or a military
; zone but Z, is not read.
def feed-822 [s]
let [c (split "," s)]
match (count c)
case 1 (feed-822-words (feed-words s))
case 2
match (feed-words (feed-at c 0))
case [d]
if (feed-822-day-name (feed-lower d))
feed-822-words (feed-words (feed-at c 1))
:none
case _ :none
case _ :none
; A date as written: [:ok t], its RFC 3339 form, or [:kept t], the text
; of one in neither form, which the render cannot read. A date with no
; text is written as a missing one is.
def feed-date [v]
match (kind v)
case :string
match v
case "" [:ok feed-render-updated]
case _
let [u (feed-upper-tz v)]
match (feed-is-3339 u)
case true [:ok u]
case false
match (feed-822 v)
case :none [:kept v]
case c [:ok c]
case _ [:kept (feed-text v)]
; ---------------------------------------------------------------------
; The check: the tree's events in, the events the router reads out
; ---------------------------------------------------------------------
; Where each member a feed can have is in the feed's slots.
def feed-slot [name]
match name
case "format" 0
case "version" 1
case "entries" 2
case "id" 3
case "title" 4
case "subtitle" 5
case "rights" 6
case "updated" 7
case "authors" 8
case "contributors" 9
case "categories" 10
case "links" 11
case "generator" 12
case "icon" 13
case "logo" 14
def feed-no-slots [:absent :absent :absent :absent :absent :absent :absent :absent :absent :absent :absent :absent :absent :absent :absent]
def feed-has [slots name]
match (get-path (path (feed-slot name)) slots)
case :present true
case _ false
; The events of what the feed lacks, and of what an entry lacks.
def feed-completions [slots]
feed-complete-updated (feed-has slots "updated")
feed-complete-title (feed-has slots "title")
feed-complete-id (feed-has slots "id") feed-render-id []
def feed-entry-completions [present pos]
feed-complete-updated (get-path (path 2) present)
feed-complete-title (get-path (path 1) present)
feed-complete-id (get-path (path 0) present) (string-join "" [feed-render-id "/" (string-join "" pos)]) []
; The root: the feed.
def feed-check-root [event]
match event
case object-start (transition [[:feed feed-no-slots false]] [event])
case array-start (feed-fail "its root is a list, where a feed is an object")
case (scalar value) (feed-fail (string-join "" ["its root is " (feed-kind value) ", where a feed is an object"]))
case _ (fail :protocol-order "the events begin with an end or a key, which a tree's never do")
; The feed: a member, or its end, before which what the feed lacks is
; handed on if it is not out yet.
def feed-in-feed [s slots done event]
match event
case (key name)
match (feed-member :feed name)
case :none (feed-fail (string-join "" ["the feed has the member " (quoted name) ", which the reader's never has"]))
case member
match (get-path (path (feed-slot name)) slots)
case :absent
if done
feed-fail (string-join "" ["the feed has the member " (quoted name) " after its entries, which every reader puts last"])
feed-feed-key s (feed-put slots (feed-slot name) :null) done member name
case _ (fail :protocol-order (string-join "" ["the events hold the feed's member " (quoted name) " twice, which a tree's never do"]))
case object-end
if done
transition [[:done]] [event]
transition [[:done]] (push event (feed-completions slots))
case _ (fail :protocol-order "the events hold a value where a key is due, which a tree's never do")
; A member of the feed: its value is due. What the feed lacks goes
; before its entries, which come last, each written as it comes.
def feed-feed-key [s slots done member name]
match name
case "entries" (transition (push [:due member name name] (feed-mark [:feed slots true] s)) (feed-completions slots))
case _ (transition (push [:due member name name] (feed-mark [:feed slots done] s)) [])
; An entry: a member, or its end, before which what it lacks is handed
; on.
def feed-in-entry [s present pos event]
match event
case (key name)
match (feed-member :entry name)
case :none (feed-fail (string-join "" ["an entry has the member " (quoted name) ", which the reader's never has"]))
case member (transition (push [:due member name name] s) [])
case object-end (transition (pop s) (push event (feed-entry-completions present pos)))
case _ (fail :protocol-order "the events hold a value where a key is due, which a tree's never do")
; A source or a person: a member, or its end.
def feed-in-object [s ctx event]
match event
case (key name)
match (feed-member ctx name)
case :none (feed-fail (string-join "" [(feed-where ctx) " has the member " (quoted name) ", which the reader's never has"]))
case member (transition (push [:due member name name] s) [])
case object-end (transition (pop s) [event])
case _ (fail :protocol-order "the events hold a value where a key is due, which a tree's never do")
; A member's value has begun and is not null: the feed or the entry it
; is a member of has it.
def feed-present [s name]
match (top s)
case [:feed slots done] (feed-mark [:feed (feed-put slots (feed-slot name) :present) done] s)
case [:entry present pos]
match name
case "id" (feed-mark [:entry (feed-put present 0 true) pos] s)
case "title" (feed-mark [:entry (feed-put present 1 true) pos] s)
case "updated" (feed-mark [:entry (feed-put present 2 true) pos] s)
case _ s
case _ s
; A member's value: its key, held until now, is handed on as `out` with
; the value's first event, and neither is where the value is null or the
; member is one not written.
def feed-due [s member name out event]
match event
case (scalar value)
match [(kind value) member]
case [:null _] (transition (pop s) [])
case [_ [:skip _]] (transition (pop s) [])
case [_ [:string _]] (transition (feed-present (pop s) name) [(key out) event])
case [_ [:date _]] (transition (feed-present (pop s) name) [(key out) event])
case _ (feed-fail (string-join "" [(feed-wants member) ", and it is " (feed-kind value)]))
case object-start
match member
case [:text el] (transition (push [:cons :text el] (feed-present (pop s) name)) [(key out) event])
case [:cons sort el] (transition (push [:cons sort el] (feed-present (pop s) name)) [(key out) event])
case [:source _] (transition (push [:obj :source] (feed-present (pop s) name)) [(key out) event])
case _ (feed-fail (string-join "" [(feed-wants member) ", and it is an object"]))
case array-start
match member
case [:list sort el] (transition (push [:items sort el ["0"]] (feed-present (pop s) name)) [(key out) event])
case _ (feed-fail (string-join "" [(feed-wants member) ", and it is a list"]))
case _ (fail :protocol-order "the events hold a key or an end where a value is due, which a tree's never do")
; A text construct, a content or a generator: a member, or its end.
def feed-in-cons [s sort el event]
match event
case (key name)
if (feed-cons-member sort name)
transition (push [:cdue sort el name] s) []
feed-fail (string-join "" ["its " el " has the member " (quoted name) ", which the reader's never has"])
case object-end (transition (pop s) [event])
case _ (fail :protocol-order "the events hold a value where a key is due, which a tree's never do")
def feed-cons-due [s sort el name event]
match event
case (scalar v)
match (kind v)
case :null (transition (pop s) [])
case _ (transition (pop s) [(key name) event])
case object-start (feed-fail (string-join "" ["its " el "'s " name " is an object, where it is text"]))
case array-start (feed-fail (string-join "" ["its " el "'s " name " is a list, where it is text"]))
case _ (fail :protocol-order "the events hold a key or an end where a value is due, which a tree's never do")
; A list: an object for each item, or its end. An entry takes the
; list's next position.
def feed-in-items [s sort el pos event]
match event
case object-start
match sort
case :person (transition (push [:obj :person] s) [event])
case :entry (transition (push [:entry [false false false] pos] (feed-mark [:items sort el (feed-next pos)] s)) [event])
case _ (transition (push [:attrs el] s) [event])
case array-end (transition (pop s) [event])
case array-start (feed-fail (string-join "" [(feed-item-name el) " is a list, where it is an object"]))
case (scalar v) (feed-fail (string-join "" [(feed-item-name el) " is " (feed-kind v) ", where it is an object"]))
case _ (fail :protocol-order "the events hold a key or an object's end inside a list, which a tree's never do")
; A category or a link: an attribute, or its end.
def feed-in-attrs [s el event]
match event
case (key name)
if (feed-attribute el name)
transition (push [:adue el name] s) []
feed-fail (string-join "" [(feed-item-name el) " has the member " (quoted name) ", which the reader's never has"])
case object-end (transition (pop s) [event])
case _ (fail :protocol-order "the events hold a value where a key is due, which a tree's never do")
def feed-attr-due [s el name event]
match event
case (scalar v)
match (kind v)
case :null (transition (pop s) [])
case _ (transition (pop s) [(key name) event])
case object-start (feed-fail (string-join "" [(feed-item-name el) "'s " name " is an object, where it is text"]))
case array-start (feed-fail (string-join "" [(feed-item-name el) "'s " name " is a list, where it is text"]))
case _ (fail :protocol-order "the events hold a key or an end where a value is due, which a tree's never do")
def feed-check-step [s event]
match (feed-top s)
case :none (feed-check-root event)
case [:done] (fail :protocol-order "the events hold more after the root value, which a tree's never do")
case [:feed slots done] (feed-in-feed s slots done event)
case [:entry present pos] (feed-in-entry s present pos event)
case [:obj ctx] (feed-in-object s ctx event)
case [:due member name out] (feed-due s member name out event)
case [:cons sort el] (feed-in-cons s sort el event)
case [:cdue sort el name] (feed-cons-due s sort el name event)
case [:items sort el pos] (feed-in-items s sort el pos event)
case [:attrs el] (feed-in-attrs s el event)
case [:adue el name] (feed-attr-due s el name event)
def feed-check-finish [s]
match (feed-top s)
case [:done] []
case :none (fail :protocol-order "the events hold no value, where a tree's hold one")
case _ (fail :protocol-order "the events ended inside a container, which a tree's never do")
def feed-check [input]
as-events (scan-emit [] feed-check-step feed-check-finish (events input))
; ---------------------------------------------------------------------
; The router: each entry on its own, and each of the feed's members
; ---------------------------------------------------------------------
def feed-captures
vector
capture :entry (path "entries" each-index) :max_record_bytes
capture :id (path "id") :max_metadata_bytes
capture :title (path "title") :max_metadata_bytes
capture :subtitle (path "subtitle") :max_metadata_bytes
capture :rights (path "rights") :max_metadata_bytes
capture :updated (path "updated") :max_metadata_bytes
capture :authors (path "authors") :max_metadata_bytes
capture :contributors (path "contributors") :max_metadata_bytes
capture :categories (path "categories") :max_metadata_bytes
capture :links (path "links") :max_metadata_bytes
capture :generator (path "generator") :max_metadata_bytes
capture :icon (path "icon") :max_metadata_bytes
capture :logo (path "logo") :max_metadata_bytes
; A member of the feed, by its capture's tag.
def feed-tag-member [tag]
match tag
case :id (feed-head-member "id")
case :title (feed-head-member "title")
case :subtitle (feed-head-member "subtitle")
case :rights (feed-head-member "rights")
case :updated (feed-head-member "updated")
case :authors (feed-head-member "authors")
case :contributors (feed-head-member "contributors")
case :categories (feed-head-member "categories")
case :links (feed-head-member "links")
case :generator (feed-head-member "generator")
case :icon (feed-head-member "icon")
case :logo (feed-head-member "logo")
; ---------------------------------------------------------------------
; The writer: the captured values in, the document's text out. What it
; is handed the check has held to a feed's shape, with no null in it.
; ---------------------------------------------------------------------
def feed-header "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<feed xmlns=\"http://www.w3.org/2005/Atom\">\n"
; An element whose text is a string, a number or a boolean.
def feed-w-string [in el v]
concat in "<" el ">" (feed-chars v) "</" el ">\n"
; A date: its RFC 3339 form, or the date a missing one is written as and
; a category that keeps the text the render cannot read, the member's
; name its term and the text its label.
def feed-w-date [in el v]
match (feed-date v)
case [:ok t] (concat in "<" el ">" t "</" el ">\n")
case [:kept t] (concat in "<" el ">" feed-render-updated "</" el ">\n" in "<category term=\"" el "\" scheme=\"" feed-date-scheme "\" label=\"" (feed-attr t) "\"/>\n")
; A member of a text construct, a content or a generator: its `value`,
; written as the element's text after its attributes, or an attribute;
; a type of xhtml is written as html.
def feed-w-cons-attr [name v]
match name
case "value" ""
case "type" (concat " type=\"" (feed-attr (feed-type v)) "\"")
case _ (concat " " name "=\"" (feed-attr v) "\"")
def feed-w-cons-end [el v]
match (kind v)
case :missing "/>\n"
case _ (concat ">" (feed-chars v) "</" el ">\n")
def feed-w-cons [in el v]
concat in "<" el (join "" (map (fn [k] (feed-w-cons-attr k (get k v))) (keys v))) (feed-w-cons-end el (get "value" v))
; A category or a link.
def feed-w-attrs [in el v]
concat in "<" el (join "" (map (fn [k] (concat " " k "=\"" (feed-attr (get k v)) "\"")) (keys v))) "/>\n"
def feed-w-person [in el v]
concat in "<" el ">\n" (join "" (map (fn [k] (feed-w-string (feed-deeper in) k (get k v))) (keys v))) in "</" el ">\n"
def feed-w-item [in sort el v]
match sort
case :person (feed-w-person in el v)
case _ (feed-w-attrs in el v)
; A member of the feed, of a source or of an entry, but a source.
def feed-w-member [in member v]
match member
case [:string el] (feed-w-string in el v)
case [:date el] (feed-w-date in el v)
case [:text el] (feed-w-cons in el v)
case [:cons _ el] (feed-w-cons in el v)
case [:list sort el] (join "" (map (fn [x] (feed-w-item in sort el x)) (as-vector v)))
case [:skip _] ""
def feed-w-source [in v]
concat in "<source>\n" (join "" (map (fn [k] (feed-w-member (feed-deeper in) (feed-head-member k) (get k v))) (keys v))) in "</source>\n"
def feed-w-entry-member [in name v]
match (feed-entry-member name)
case [:source _] (feed-w-source in v)
case member (feed-w-member in member v)
def feed-w-entry [in v]
concat in "<entry>\n" (join "" (map (fn [k] (feed-w-entry-member (feed-deeper in) k (get k v))) (keys v))) in "</entry>\n"
; The author a feed with none is written with: named by the feed's
; title where that is text and not empty, `unknown` otherwise, and the
; render's id as its uri, which marks it.
def feed-render-author "unknown"
def feed-w-author [title]
concat " <author>\n <name>" (feed-chars (match title (case "" feed-render-author) (case _ title))) "</name>\n <uri>" feed-render-id "</uri>\n </author>\n"
def feed-has-items [v]
match (kind v)
case :vector
match (compare (count (as-vector v)) 0)
case :greater true
case _ false
case _ false
; A title's text, where it is text: of type text, or of no type.
def feed-title-text [v]
match [(kind (get "value" v)) (kind (get "type" v))]
case [:string :missing] (get "value" v)
case [:string _]
match (get "type" v)
case "text" (get "value" v)
case _ ""
case _ ""
; The writer's state once it has begun: whether the feed's author is
; written and its title's text.
def feed-note [s tag v]
match [tag s]
case [:authors [:open _ title]] [:open (feed-has-items v) title]
case [:title [:open author _]] [:open author (feed-title-text v)]
case _ s
def feed-write-item [s item out]
match [s item]
case [[:open author title] (selected :entry v)]
if author
transition [:entries] (push (feed-w-entry " " v) out)
transition [:entries] (push (feed-w-entry " " v) (push (feed-w-author title) out))
case [_ (selected :entry v)] (transition s (push (feed-w-entry " " v) out))
case [_ (selected tag v)] (transition (feed-note s tag v) (push (feed-w-member " " (feed-tag-member tag) v) out))
; Each captured value as it comes, the document's start before the first;
; the first entry written as it comes has the render's author before it
; where the feed's is not written, since what follows cannot be seen.
def feed-write-step [s item]
match s
case [:start] (feed-write-item [:open false ""] item [feed-header])
case _ (feed-write-item s item [])
def feed-write-finish [s]
match s
case [:start] [feed-header (feed-w-author "") "</feed>\n"]
case [:open author title]
if author
["</feed>\n"]
[(feed-w-author title) "</feed>\n"]
case _ ["</feed>\n"]
; The render: a feed tree's events in, the Atom document's text out.
def feed-render [input]
join ""
scan-emit [:start] feed-write-step feed-write-finish
route feed-captures (feed-check input)