summaryrefslogtreecommitdiff
path: root/desk/lib/sortug.hoon
blob: 70aa3b8f2311dddaffc68337315f432a319b1295 (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
::  Painstakingly built utility functions by Sortug Development Ltd.
::  There's more where it came from
|%
++  jikan
|%
++  from-unix  |=  ts=@  ^-  @da
  (from-unix:chrono:userlib ts)
++  to-unix-ms  |=  da=@da  ^-  @ud
  (unm:chrono:userlib da)
++  to-unix-secs  |=  da=@da  ^-  @ud
  (unt:chrono:userlib da)
--
++  b64  (bass 64 (plus siw:ab))
++  b16  (bass 16 (plus six:ab))
++  scow
=|  min-chars=@ud
|=  [mod=@tas a=@]  ^-  tape
  ?+  mod  ""
  %s   (signed-scow a)
  %ud  (a-co:co a)
  %ux  ((x-co:co min-chars) a)
  %uv  ((v-co:co min-chars) a)
  %uw  ((w-co:co min-chars) a)
  ==
++  signed-scow  |=  a=@s  ^-  tape
  =/  old  (old:si a)
  =/  num  (scow %ud +.old)
  =/  sign=tape  ?:  -.old  ""  "-"
  "{sign}{num}"
++  slaw
  |=  [mod=@tas txt=@t]  ^-  (unit @)
  ?+  mod  ~
  %ud  (rush txt dem)
  %ux  (rush txt b16)
  %uv  (rush txt vum:ag)
  %uw  (rush txt b64)
  ==
::  secs
++  timestamp  |=  s=@t  ^-  (unit @da)
  =/  un  (slaw %ud s)  ?~  un  ~
  =/  secs  (from-unix:jikan u.un)
  (some secs)


++  csplit  |*  =rule  
  (more rule (cook crip (star ;~(less rule next))))
:: List utils
++  foldi
  |*  [a=(list) b=* c=_|=(^ +<+)]
  =|  i=@ud
  |-  ^+  b 
  ?~  a  b
  =/  nb  (c i i.a b)
  $(a t.a, b nb, i +(i))
++  parsing
  |%
  ++  link  auri:de-purl:html
  ++  para
    |%
    ++  eof        ;~(less next (easy ~))
    ++  white      (mask "\09 ")
    ++  blank      ;~(plug (star white) (just '\0a'))
    ++  hard-wrap  (cold ' ' ;~(plug blank (star white)))
    ++  one-space  (cold ' ' (plus white))
    ++  empty
      ;~  pose
        ;~(plug blank (plus blank))
        ;~(plug (star white) eof)
        ;~(plug blank (star white) eof)
      ==
    ++  para
      %+  ifix
        [(star white) empty]
      %-  plus
      ;~  less
        empty
        next
      ==
    --
  ++  trim  para:para  ::  from whom/lib/docu
  ++  youtube
    ;~  pfix
      ;~  plug
          (jest 'https://')
          ;~  pose
              (jest 'www.youtube.com/watch?v=')
              (jest 'youtube.com/watch?v=')
              (jest 'youtu.be/')
          ==
      ==
      ;~  sfix
          (star aln)
          (star next)
      ==
    ==
  ++  twatter
    ;~  pfix
      ;~  plug
          (jest 'https://')
          ;~  pose
              (jest 'x.com/')
              (jest 'twitter.com/')
          ==
          (star ;~(less fas next))
          (jest '/status/')
      ==
      ;~  sfix
          (star nud)
          (star next)
      ==
    ==
  ++  img-set
    %-  silt
    :~  ~.webp
        ~.png
        ~.jpeg
        ~.jpg
        ~.svg
    ==
  ++  is-img
  |=  t=@ta
    (~(has in img-set) t)
  ++  is-image
  |=  url=@t  ^-  ?
    =/  u=(unit purl:eyre)  (de-purl:html url)
      ?~  u  .n
    =/  ext  p.q.u.u
    ?~  ext  .n
    (~(has in img-set) u.ext)
  --
++  string
|%
++  replace
  |=  [bit=tape bot=tape =tape]
  ^-  ^tape
  |-
  =/  off  (find bit tape)
  ?~  off  tape
  =/  clr  (oust [(need off) (lent bit)] tape)
  $(tape :(weld (scag (need off) clr) bot (slag (need off) clr)))
  ::
++  split
  |=  [str=tape delim=tape]
    ^-  (list tape)
    (split-rule str (jest (crip delim)))
  ++  split-rule
    |*  [str=tape delim=rule]
    ^-  (list tape)
    %+  fall
      (rust str (more delim (star ;~(less delim next))))
    [str ~]
--
--