urbit/gen/marc.hoon

357 lines
11 KiB
Plaintext
Raw Normal View History

2017-07-16 03:59:57 +03:00
::
:::: hoon/marc/gen
::
2017-07-18 04:07:47 +03:00
:- %say
|= *
:- %noun
2017-07-16 03:59:57 +03:00
=> |%
2017-07-18 04:07:47 +03:00
++ item (pair mite (list flow)) :: xml node generator
2017-07-16 03:59:57 +03:00
++ colm @ud :: column
2017-07-20 07:48:00 +03:00
++ flow (each manx twig) :: node or generator
2017-07-16 03:59:57 +03:00
++ form (unit $?($emph $bold $code)) :: formatting
2017-07-18 04:07:47 +03:00
++ mite :: context
$? $down :: outer embed
$list :: unordered list
$lime :: list item
$lord :: ordered list
2017-07-19 06:19:27 +03:00
$poem :: verse
2017-07-18 04:07:47 +03:00
$bloc :: blockquote
$code :: preformatted code
2017-07-19 06:19:27 +03:00
$expr :: dynamic expression
2017-07-18 04:07:47 +03:00
==
2017-07-16 03:59:57 +03:00
++ trig :: line style
$: col/@ud :: start column
$= sty :: style
2017-07-20 07:48:00 +03:00
$? $done :: terminator
2017-07-16 03:59:57 +03:00
$none :: end of input
$lint :: + line item
$lite :: - line item
$head :: # heading
$text :: anything else
== == ::
++ word @t :: source text
--
2017-07-20 07:48:00 +03:00
|% ::
++ cram :: markdown with errors
2017-07-16 03:59:57 +03:00
|= {naz/hair los/tape}
^- (like item)
::
2017-07-18 04:07:47 +03:00
:: err: error position
2017-07-16 03:59:57 +03:00
:: col: current control column
:: hac: stack of items under construction
:: cur: current item under construction
2017-07-20 07:48:00 +03:00
:: lub: current block being read in
2017-07-16 03:59:57 +03:00
::
2017-07-18 04:07:47 +03:00
=| err/(unit hair)
=/ col q.naz
2017-07-16 03:59:57 +03:00
=| hac/(list item)
2017-07-20 07:48:00 +03:00
=/ cur/item [%down ~]
2017-07-16 03:59:57 +03:00
=| lub/(unit (pair hair (list tape)))
2017-07-18 04:07:47 +03:00
=< $:line
2017-07-16 03:59:57 +03:00
|%
:: ::
2017-07-20 07:48:00 +03:00
++ $ :: resolve
2017-07-18 04:07:47 +03:00
^- (like item)
2017-07-20 07:48:00 +03:00
:: if error position is set, produce error
2017-07-16 03:59:57 +03:00
::
2017-07-20 07:48:00 +03:00
?. =(~ err) [+.err ~]
:: all data was consumed
2017-07-16 03:59:57 +03:00
::
2017-07-20 07:48:00 +03:00
=- [naz `[- [naz los]]]
|- ^- item
:: fold all the way to top
2017-07-16 03:59:57 +03:00
::
2017-07-20 07:48:00 +03:00
?~ hac cur
$(..^$ fold)
2017-07-16 03:59:57 +03:00
:: ::
++ back :: column retreat
2017-07-20 07:48:00 +03:00
|= luc/@ud
2017-07-18 04:07:47 +03:00
^+ +>
2017-07-20 07:48:00 +03:00
?: =(luc col) +>
:: nex: next backward step that terminates this context
::
=/ nex/@ud
?- p.cur
$down 0
$expr 2
$list 0
$lime 2
$lord 0
$poem 8
$code 4
$bloc 6
==
?: (gth nex (sub col luc))
:: indenting pattern violation
::
..^$(err `[p.naz luc])
$(..^$ fold, col (sub col nex))
2017-07-16 03:59:57 +03:00
:: ::
2017-07-20 07:48:00 +03:00
++ fine :: item to flow
^- flow
=- [%& [[- ~] q.cur]]
:: only items which contain flows
::
?+ p.cur !!
$list %ul
$lord %ol
$lime %li
$bloc %bq
$code %pre
==
:: ::
++ fold ^+ . :: complete and pop
?~ hac .
%= .
hac t.hac
cur [p.i.hac [fine q.i.hac]]
==
:: ::
++ snap :: capture raw line
2017-07-16 03:59:57 +03:00
=| nap/tape
2017-07-20 07:48:00 +03:00
|- ^+ [nap +>]
:: no unterminated lines
::
?~ los [~ +>(err `naz)]
?: =(`@`10 i.los)
:: consume newline
::
:_ +>(los t.los, naz [+(p.naz) 1])
:: trim trailing spaces
::
|- ^- tape
?: ?=({$' ' *} nap)
$(nap t.nap)
(flop nap)
$(los t.los, q.naz +(q.naz), nap [i.los nap])
2017-07-16 03:59:57 +03:00
:: ::
2017-07-18 04:07:47 +03:00
++ skip +:snap :: discard line
2017-07-19 06:19:27 +03:00
++ look :: inspect line
^- (unit trig)
2017-07-20 07:48:00 +03:00
?~ los
`[q.naz %none]
?: =(`@`10 i.los)
~
?: =(' ' i.los)
look(los t.los, q.naz +(q.naz))
:- q.naz
?: =('\\' i.los)
%done
?: =('\\' i.los)
%head
?: ?=({$'-' $' ' *} los)
%lite
?: ?=({$'-' $' ' *} los)
%lint
%text
:: ::
++ cape :: xml-escape
|= tex/tape
^- tape
?~ tex tex
=+ $(tex t.tex)
?+ i.tex [i.tex -]
$34 ['&' 'q' 'u' 'o' 't' ';' -]
$38 ['&' 'a' 'm' 'p' ';' -]
$39 ['&' '#' '3' '9' ';' -]
$60 ['&' 'l' 't' ';' -]
$62 ['&' 'g' 't' ';' -]
==
:: ::
++ made :: compose block
2017-07-16 03:59:57 +03:00
^+ .
2017-07-20 07:48:00 +03:00
:: empty block, no action
::
?~ lub .
:: if block is preformatted code
::
?: ?=($code p.cur)
:: add blank line between blocks
::
=. q.cur
?~ q.cur q.cur
:_(q.cur `manx`[[%$ [%$ `tape`[`@`10 ~]]] ~])
%= .
q.cur
%+ weld
%+ turn
q.u.lub
|= tape ^- flow
:: each line is text data with its newline
::
:- %&
[[%$ [%$ (weld (slag col +<) `tape`[`@`10 ~]]] ~])
q.cur
==
:: if block is verse
::
?: ?=($poem p.cur)
:: add break between stanzas
::
=. q.cur ?~(q.cur q.cur [[[%br ~] ~] q.cur])
%= .
q.cur
%+ weld
%+ turn
q.u.lub
|= tape ^- flow
:: each line is a paragraph
::
:- %&
:- [%p ~]
:_ ~
[[%$ [%$ (weld (slag col +<) `tape`[`@`10 ~]]] ~])
q.cur
==
:: yex: block recomposed for reparsing
::
=/ yex/tape (zing (flop q.u.lub))
:: if block is dynamic
::
?: ?=($expr p.cur)
2017-07-18 04:07:47 +03:00
:: sab: rule for embedded twig
::
2017-07-20 07:48:00 +03:00
=* sab (ifix [gay ;~(plug (star ace) (just `@`10))] tall:vast)
2017-07-18 04:07:47 +03:00
:: vex: product of parsing following twig
::
2017-07-20 07:48:00 +03:00
=/ vex/(like twig) (sab naz los)
2017-07-18 04:07:47 +03:00
:: fail upward if parse failed
::
2017-07-20 07:48:00 +03:00
?~ q.vex ..^$(err `p.vex)
2017-07-18 04:07:47 +03:00
:: otherwise, add item and continue
:: ::
:: %= $
:: naz p.q.u.vex
:: los q.q.u.vex
:: lap :_(lap [%| %marl p.u.vex])
:: ==
2017-07-16 03:59:57 +03:00
:: ::
2017-07-18 04:07:47 +03:00
++ line ^+ . :: body line loop
2017-07-16 03:59:57 +03:00
:: abort after first error
::
2017-07-19 06:19:27 +03:00
?: !=(~ err) .
2017-07-16 03:59:57 +03:00
:: pic: profile of this line
::
=/ pic look
:: if line is blank
::
2017-07-19 06:19:27 +03:00
?~ pic
2017-07-16 03:59:57 +03:00
:: break section
::
2017-07-19 06:19:27 +03:00
line:made:skip
2017-07-16 03:59:57 +03:00
:: line is not blank
::
=> .(pic u.pic)
2017-07-18 04:07:47 +03:00
:: if end of input, complete
2017-07-16 03:59:57 +03:00
::
2017-07-18 04:07:47 +03:00
?: ?=($none sty.pic)
..$(q.naz col.pic)
:: if end marker behind current column
2017-07-16 03:59:57 +03:00
::
2017-07-19 06:19:27 +03:00
?: &(?=($done sty.pic) (lth col.pic col))
2017-07-18 04:07:47 +03:00
:: retract and complete
2017-07-16 03:59:57 +03:00
::
2017-07-18 04:07:47 +03:00
(back(q.naz (add 2 col.pic)) col.pic)
2017-07-19 06:19:27 +03:00
:: bal: inspection copy of lub, current section
::
=/ bal lub
2017-07-18 04:07:47 +03:00
:: if within section
2017-07-16 03:59:57 +03:00
::
2017-07-19 06:19:27 +03:00
?^ bal
2017-07-18 04:07:47 +03:00
:: detect bad block structure
2017-07-16 03:59:57 +03:00
::
2017-07-18 04:07:47 +03:00
?: ?| ?=($head sty.pic)
?: ?=(?($code $poem $expr) p.cur)
(lth col.pic col)
2017-07-19 06:19:27 +03:00
|(!=(%text sty.pic) !=(col.pic col))
2017-07-18 04:07:47 +03:00
==
..$(err `[p.naz col.pic])
:: accept line and continue
::
2017-07-20 07:48:00 +03:00
=^ nap ..$ snap
2017-07-19 06:19:27 +03:00
line(lub bal(q.u [nap q.u.bal]))
2017-07-18 04:07:47 +03:00
:: if column has retreated, adjust stack
2017-07-16 03:59:57 +03:00
::
2017-07-18 04:07:47 +03:00
=. ..$ ?: (lth col.pic col) ..$
(back col.pic)
:: dif: columns advanced
2017-07-19 06:19:27 +03:00
:: erp: error position
2017-07-16 03:59:57 +03:00
::
2017-07-18 04:07:47 +03:00
=/ dif (sub col.pic col)
2017-07-19 06:19:27 +03:00
=/ erp [p.naz col.pic]
2017-07-18 04:07:47 +03:00
=. col col.pic
:: nap: take first line
::
=^ nap ..$ snap
:: execute appropriate paragraph form
::
2017-07-19 19:53:34 +03:00
=< line:abet:apex
2017-07-18 04:07:47 +03:00
|%
:: ::
++ abet :: accept line
..$(lub `[naz nap ~])
:: ::
2017-07-19 06:19:27 +03:00
++ apex ^+ . :: by column offset
2017-07-18 04:07:47 +03:00
?+ dif fail
$0 apse
$2 expr
$4 code
$6 bloc
$8 poem
==
:: ::
2017-07-19 06:19:27 +03:00
++ apse ^+ . :: by prefix style
2017-07-18 04:07:47 +03:00
?- sty.pic
2017-07-19 06:19:27 +03:00
$done !!
2017-07-18 04:07:47 +03:00
$head head
$lite lite
$lint lint
$text text
==
:: ::
++ bloc apse:(push %bloc) :: blockquote line
2017-07-19 06:19:27 +03:00
++ fail .(err `erp) :: set error position
2017-07-20 07:48:00 +03:00
++ push |=(mite %_(+> hac [cur hac], cur [+< ~])) :: push context
2017-07-18 04:07:47 +03:00
++ expr (push %expr) :: hoon expression
++ code (push %code) :: code literal
++ poem (push %poem) :: verse literal
++ head !!
++ lent :: list entry
|= ord/?
2017-07-19 06:19:27 +03:00
^+ +>
2017-07-18 04:07:47 +03:00
:: erase list marker
::
=. nap =+(+(col) (runt [- ' '] (slag - nap)))
:: indent by 2
::
=. col (add 2 col)
:: can't switch list types
::
?: =(?:(ord %list %lord) p.cur) fail
2017-07-20 07:48:00 +03:00
:: push list item
::
%. %lime
=< push
:: push list context, unless we're in list
2017-07-18 04:07:47 +03:00
::
=+ ?:(ord %lord %list)
2017-07-20 07:48:00 +03:00
?: =(- p.cur) ..push (push -)
2017-07-18 04:07:47 +03:00
::
++ lint (lent &) :: numbered list
++ lite (lent |) :: unnumbered list
++ text :: plain text
2017-07-19 06:19:27 +03:00
^+ .
2017-07-18 04:07:47 +03:00
:: except in lists, continue in current flow
::
?. ?=(?($list $lord) p.cur) .
2017-07-20 07:48:00 +03:00
:: in lists, complete current list and pop
2017-07-18 04:07:47 +03:00
::
?> ?=(^ hac)
2017-07-20 07:48:00 +03:00
%= .
hac t.hac
cur [p.i.hac [[%& cur] q.i.hac]]
==
2017-07-18 04:07:47 +03:00
--
2017-07-16 03:59:57 +03:00
--
2017-07-18 04:07:47 +03:00
--