summaryrefslogtreecommitdiff
path: root/desk/lib/trill/ui.hoon
blob: 8f059362a276137c06e10b357a7e21e1c6bde751 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/-  tp=trill-post
/+  sr=sortug
:: /+  md=markdown
|%
++  abbr
|=  [t=@t c=@ud]  ^-  @t
  =/  size  (met 3 t)
  ?:  (lte size c)  t
  %^  cat  3
  (cut 3 [0 c] t)
  '...'
++  abbr-t
|=  [t=tape c=@ud]  ^-  tape
  =/  size  (lent t)
  ?:  (lte size c)  t
  %+  weld  (scag c t)
  "..."
::  tape -> post:trill, parsing user input from Sail
++  parse  :: Markdown parser. Actually udon parser but it'll do
|=  s=tape  ^-  (unit marl:hoot) :: finally
:: TODO check out parse:chat-graph.hoon
:: Annoying it requires a line break but then parses it as a space wtf
=,  vast
(rust s cram:(sail .y))
+$  heading  $?(%h1 %h2 %h3 %h4 %h5 %h6)
++  tokenize  
|=  s=@t  ^-  content-list:tp
  =/  t  (weld (trip s) "\0a")
  =/  parsed  (parse t)
  :: =/  parsed2  (parse:md t)
  :: ~&  >    diary-parser=parsed2
  ::  \0a can't be followed by a space. ever. those are the rules
  ?~  parsed  ~&  error-parsing-markdown=t  ~
  (marl-to-cl u.parsed)
++  marl-to-cl
|=  =marl:hoot  ^-  content-list:tp
  %-  flop
  %+  roll  marl
  |=  [=tuna:hoot acc=content-list:tp]
  :: man this is an annoying type if I ever saw one
  ?@  -.tuna  acc
  =/  blk  (manx-to-block tuna)
  ?~  blk  acc  :_  acc  u.blk
++  manx-to-block
  |=  =manx:hoot  ^-  (unit block:tp)
  ?+  n.g.manx  ~
  heading      %-  some   [%heading (phead n.g.manx c.manx)]
  %p           %-  some   [%paragraph (inline-list c.manx)]
  %blockquote  %-  some   [%blockquote (inline-list c.manx)]
  %pre         %-  some   [%codeblock (pre c.manx)]
  %hr          %-  some   [%paragraph ~[[%break ~]]]
  %ul          %-  some   [%list (list-items c.manx) .n]
  %ol          %-  some   [%list (list-items c.manx) .y]
  :: %table       %-  some   (table-rows c.manx)
  ==
:: ++  table-row
:: |=  =manx:hoot  ^-  (list cell:table:tp)
:: ?.  ?=(%td n.g.manx)  ~

:: ++  table-rows
:: |=  =marl:hoot  ^-  table:tp
:: =/  a
:: %+  roll  marl  |=  [=manx:hoot acc=table:tp]
:: :-  %table
:: ?:  ?=(%th n.g.manx)  :_  rows.acc  [(marl-to-cl c.manx) headers.acc]
:: ?.  ?=(%tr n.g.manx)  :-  headers.acc
:: :-  headers.acc  rows.acc

++  list-items
|=  =marl:hoot  ^-  (list li:clist:tp)
%-  flop
%+  roll  marl  |=  [=tuna:hoot acc=(list li:clist:tp)]
?@  -.tuna  acc
?.  ?=(%li n.g.tuna)  acc  :_  acc  (marl-to-cl c.tuna)
++  phead
|=  [h=heading c=marl:hoot]  ^-  [p=cord q=heading]
:-  (get-tag-text c)  h
++  inline-list
  |=  c=marl:hoot  ^-  (list inline:tp)
  %-  flop
  %+  roll  c
  |=  [=tuna:hoot acc=(list inline:tp)]
  ?@  -.tuna  acc  :_  acc  (inline tuna)
  ++  inline
  |=  =manx:hoot  ^-  inline:tp
  ?:  ?=(%$ n.g.manx)  [%text (get-attrs-text a.g.manx)]
  =/  text=@t  (get-tag-text c.manx)
  ?+  n.g.manx  [%text text]
  %i     [%italic text]
  %b     [%bold text]
  %code  [%codespan text]
  %br    [%break ~]
  %a    :+  %link  (get-attrs-text a.g.manx)  (get-tag-text c.manx)
  %img  :+  %link  (get-attr-text a.g.manx %src)  (get-attr-text a.g.manx %alt)
  ==
::
++  reduce-block
|=  c=marl:hoot  ^-  @t
%+  roll  c
|=  [=tuna:hoot acc=@t]
?@  -.tuna  acc
?+  n.g.tuna  acc
%p  (get-tag-text c.tuna)
==
++  get-attr-text
|=  [a=mart:hoot attr=@tas]  ^-  @t
%-  crip  %-  flop
%+  roll  a
|=  [[n=mane v=(list beer:hoot)] acc=tape]
?.  .=(attr n)  acc
%+  roll  v
|=  [b=beer:hoot acc=tape]
?^  b  acc  [b acc]
++  get-attrs-text  :: this assumes we don't care about which attr, which we usually don't
|=  a=mart:hoot  ^-  @t
:: ?:  (gte (lent a) 1)  
%-  crip  %-  flop
%+  roll  a
|=  [[n=mane v=(list beer:hoot)] acc=tape]
%+  roll  v
|=  [b=beer:hoot acc=tape]
?^  b  acc  [b acc]
++  get-tag-text
|=  c=marl:hoot  ^-  @t
::  there's only really one child in these things
%+  roll  c
|=  [=tuna:hoot acc=@t]
?@  -.tuna  acc
%-  crip
%-  flop
%+  roll  a.g.tuna
|=  [[n=mane v=(list beer:hoot)] acc=tape]
%+  roll  v
|=  [b=beer:hoot acc=tape]
?^  b  acc  [b acc]

++  pre
|=  c=marl:hoot  ^-  [cord cord]
:_  ''  :: lang not supported, duh
%+  roll  c
|=  [=tuna:hoot acc=@t]
?@  -.tuna  acc
(get-attrs-text a.g.tuna)


++  parse-tags
|=  t=@t  ^-  (unit (set @t))
  =/  lst  (rush t (csplit:parsing:sr com))
  ?~  lst  ~  (some (sy u.lst))
++  tape-to-cm
|=  =tape  ^-  content-map:tp
(gas:corm:tp *content-map:tp [*@da (tokenize (crip tape))]~)
:: post:trill -> manx

:: post:trill -> (markdown) tape for display on sail
++  block-to-md
|=  b=block:tp  ^-  tape
  ?+  -.b  ""
%paragraph    
  %^  foldi:sr  p.b  ""  |=  [in=@ud i=inline:tp acc=tape]
    =/  il  (inline-to-tape i)  
    ?:  .=(+(in) (lent p.b))
    "{acc}{il}"
    "{acc}{il} "
%blockquote   
  %+  weld  "> "
  %^  foldi:sr  p.b  ""  |=  [in=@ud i=inline:tp acc=tape]
  =/  il  (inline-to-tape i)  
  ?:  .=(+(in) (lent p.b))
  "{acc}{il}"
  "{acc}{il} "
%list  
  %^  foldi:sr  p.b  ""  |=  [in=@ud =li:tp acc=tape]
  =/  li-tape  (content-list-to-md li)
  =/  line  ?:  ordered.b  
  "{<+(in)>}. {li-tape}"
  "- {li-tape}"
  ?:  .=(+(in) (lent p.b))
  "{acc}{line}"
  "{acc}{line}\0a"
%media  
  ?+  -.media.b  "![{(trip p.media.b)}]({(trip p.media.b)})"
%images  %^  foldi:sr  p.media.b  ""  |=  [i=@ud [url=@t caption=@t] acc=tape]
  =/  line  "![{(trip caption)}]({(trip url)})"
  ?:  .=(+(i) (lent p.media.b))
  "{acc}{line}"
  "{acc}{line}\0a"
  ==
%codeblock
  """
  ```
  {(trip code.b)}
  ```
  """
%heading  =/  dashes=tape  ?-  q.b
  %h1  "# "
  %h2  "## "
  %h3  "### "
  %h4  "#### "
  %h5  "##### "
  %h6  "###### "
  ==  "{dashes}{(trip p.b)}"
%tasklist  ""  ::TODO
  ::
  :: %table  acc
  :: %eval  acc
  :: %ref   acc
  :: %json  acc
  ==  
++  content-list-to-md
|=  =content-list:tp  ^-  tape
  %^  foldi:sr  content-list  ""  |=  [i=@ud b=block:tp acc=tape]
  =/  block-tape  (block-to-md b)
  ?:  .=(+(i) (lent content-list))
  "{acc}{block-tape}"
  "{acc}{block-tape}\0a\0a"
++  content-to-md
|=  c=content-map:tp  ^-  tape
  ?~  c  ""
  =/  latest=(list block:tp)  val:head:(pop:corm:tp c)
  (content-list-to-md latest)
++  inline-to-tape
|=  i=inline:tp  ^-  tape
  ?-  -.i  
  %text  (trip p.i)
  %italic  "_{(trip p.i)}_"
  %bold    "*{(trip p.i)}*" 
  %strike   "~~{(trip p.i)}~~"
  %ship     (scow %p p.i)
  %codespan   "`{(trip p.i)}`"  
  %link   "[{(trip show.i)}]({(trip href.i)})"
  %break  "\0a"
  :: TODO custom syntax
  %date  
  =/  t  (time:enjs:format p.i)
  ?.  ?=(%n -.t)  ""  (trip p.t)
  %note  ""  :: TODO
  %underline   (trip p.i)
  %sup   (trip p.i)
  %sub   (trip p.i)
  %ruby   (trip p.i) 
  ==
++  tags-to-tape
|=  t=(set @t)  ^-  tape
  %^  foldi:sr  ~(tap in t)  ""  |=  [i=@ud c=@t acc=tape]
  ?:  .=(+(i) ~(wyt in t))
  "{acc}{(trip c)}"
  "{acc}{(trip c)},"
--