all repos — h3rald @ e5ff224a96fb40ab4558873dd976b3abd131a3f5

The sources of https://h3rald.com

rules.min

 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
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
'hastysite import

{} :ARCHIVES
() :ARTICLES
{} :HOME
() :PROJECTS
() :SPELLS

"contents/articles" sys.ls size :articles-count
17 :projects-count
"contents/grimoire" sys.ls size :grimoire-count

(
  symbol to-date
  (int :timestamp ==> str :result)
  (
    time.stamp integer "dddd, d MMMM yyyy" time.format @result
  )
) ::
;; Utility operator to format a timestamp into a date


(
  symbol create-articles-page
  (==>)
  (
    {}
    "articles/index"    "id" dict.set
    "articles.html"     "path" dict.set
    ".html"             "ext" dict.set
    "ARTICLES"          "title" dict.set
    "page"              "content-type" dict.set
    ARCHIVES dict.values ("code" dict.get :a "code" dict.get :b a b >) sort 
    ;; sort articles within each archive
    (
      stack.dup "articles" dict.get 
        ("timestamp" dict.get :a "timestamp" dict.get :b
        a b >) sort "articles" dict.set
    ) map
    "archives" dict.set
    projects-count "projects-count" dict.set
    articles-count "articles-count" dict.set
    grimoire-count "grimoire-count" dict.set
    ;; TODO: why processing twice??
    stack.dup "_archives" stack.swap mustache "contents" dict.set
    stack.dup "page" stack.swap mustache :page
    "articles" page process-icons
    "contents" dict.set
    output-fwrite
  )
) ::
;; Create articles page

(
  symbol create-projects-page
  (==>)
  (
    {}
    "projects/index"                                    "id" dict.set
    "projects.html"                                     "path" dict.set
    ".html"                                             "ext" dict.set
    "PROJECTS"                                          "title" dict.set
    "page"                                              "content-type" dict.set
    PROJECTS ('active dict.has?) filter ("id" dict.get stack.swap "id" dict.get <) sort     "active-projects" dict.set
    PROJECTS ('active dict.has? not) filter ("id" dict.get stack.swap "id" dict.get <) sort "inactive-projects" dict.set
    projects-count "projects-count" dict.set
    articles-count "articles-count" dict.set
    grimoire-count "grimoire-count" dict.set
    stack.dup "projects" stack.swap mustache :page
    "projects" page process-icons
    "contents" dict.set
    output-fwrite
  )
) ::
;; Create the Projects page

(
  symbol create-grimoire-page
  (==>)
  (
    {}
    "grimoire/index"                                    "id" dict.set
    "grimoire.html"                                     "path" dict.set
    ".html"                                             "ext" dict.set
    "GRIMOIRE"                                          "title" dict.set
    "page"                                              "content-type" dict.set
    SPELLS ("id" dict.get stack.swap "id" dict.get <) sort                        "spells" dict.set
    projects-count "projects-count" dict.set
    articles-count "articles-count" dict.set
    grimoire-count "grimoire-count" dict.set
    stack.dup "grimoire" stack.swap mustache :page
    "grimoire" page process-icons
    "contents" dict.set
    output-fwrite
  )
) ::
;; Create the Grimoire page

(
  symbol process-timestamp
  (dict :content ==> dict :result)
  (
    (content 'timestamp dict.has?)
    (
      content "timestamp" dict.get :ts
      ts to-date :date 
      ts integer "yyyyMM" time.format :code
      ts integer "MMMM yyyy" time.format :month
      ts integer "MMMM-yyyy" time.format lowercase :id
      ;; Add to articles
      content date "date" dict.set @content
      content ARTICLES append @ARTICLES
      (ARCHIVES code dict.has?)
      (
        ;; Add article to existing archive
        content ("id" "title" "timestamp" "date") dict.pick :article
        ARCHIVES code dict.get :archivedata
        archivedata "articles" dict.get :articles
        article articles append @articles
        archivedata articles "articles" dict.set :archivedata
        ARCHIVES archivedata code dict.set @ARCHIVES
      )
      (
        ;; Create new monthly archive
        content ("id" "title" "timestamp" "date") dict.pick :article
        {}
        month         "month" dict.set
        code          "code" dict.set
        article quote "articles" dict.set
        id            "id" dict.set
        :archive
        ARCHIVES archive code dict.set @ARCHIVES
      ) if
    ) when
    content @result
  )
) ::
;; Group articles by month

(
  symbol copy-pdf-article-asset
  (dict :asset ==>)
  (
    asset "id" dict.get :id
    id "^pdf/(.+)$" search 1 get :name
    asset "output/articles/$1/$1" (name) => % "id" dict.set @asset
    asset output-cp
  )
) ::
;; Copy PDF files saved in asset folder
;; to the corresponding article output folder.

;; Syntax highlighting
(
  symbol process-code
  (str :text ==> str :result)
  (
    "temp/highlight" :tmpfile
    "chroma $# $#--html --html-only --html-prevent-surrounding-pre" :cmd
    text "<pre>\\s*<code[^>]*?(?:class=\"([a-z0-1]+)\")?>([\\s\\S]+?)</code></pre>" (
      :match
      match 2 get :code
      match 1 get :lang
      "" :lexer
      (lang "" !=) 
        ("--lexer $# " (lang) =% @lexer)
      when
      ;; Unescape HTML entities
      code "(&gt;|&lt;|&quot;|&amp;)" (
        :match
        match 1 get :ntt
        (ntt "&gt;" ==)
          (">")
        when
        (ntt "&lt;" ==)
          ("<")
        when
        (ntt "&quot;" ==)
          ("\"")
        when
        (ntt "&amp;" ==)
          ("&")
        when
      ) replace-apply @code
      code tmpfile fs.write
      cmd (tmpfile lexer) =% sys.run "output" dict.get :out
      "<pre class=\"chroma\"><code>$#</code></pre>" (out) =%
    ) replace-apply @result
  )
) ::

(
  symbol prepend-toc
  (str :text ==> str :result)
  (
    () :headings
    text "<h3[^>]*>([^<]+)</h3>" (
      :matches
      matches 1 get :title
      title "[^a-zA-Z0-9_-]" "_" replace :id
      "heading__$#" (id) =% @id
      {}
        title "title" dict.set
        id "id" dict.set 
      headings append @headings
      "<a class='heading-anchor' id='$1'></a><h3>$2 <a class='go2top' href='#top' title='Go to the top'>⇈</a></h3>" (id title) =%
    ) replace-apply @result
    (headings size 3 >=)
      (
        headings (
          :heading
          heading "id" dict.get :id
          heading "title" dict.get :text
          "<li><a href='#$1'>$2</a></li>" (id text) =%
        ) map "\n" join :links
        "<ul id='toc' class='panel'>$1</ul>" (links) =% :toc
        "$1\n$2" (toc result) =% @result
      )
    when
  )
) ::

;; Process :icon: markup
(
  symbol process-icons
  (str :id str :text ==> str :result)
  ( 
    ;; whitelist of common false positives for icon markup
    ("borrow", "http", "cakephp") :whitelist
    text ":([a-z]+[a-z0-9-]+)(?:\\|([a-zA-Z0-9 ]+))?:" (
      :match
      match 0 get :content
      match 1 get :icon
      match 2 get :explicit-title
      "assets/images/icons/$#.svg" (icon) =% :path
      (path fs.file?)
        (
          path fs.read xml.from-xml "svg" xml.query :svgx
          icon :title
          (explicit-title "" !=)
            (explicit-title @title)
          when
          ;; Add title tag
          "<title>$#</title>" (title) =% xml.from-xml :svg-title
          svgx stack.dup "children" dict.get svg-title stack.swap prepend "children" dict.set @svgx
          ;; Add role="img"
          svgx "attributes" dict.get :svg-attrs
          svgx svg-attrs "img" "role" dict.set "attributes" dict.set @svgx
          svgx xml.to-xml :svg
          "<span class=\"icon\">$#</span>" (svg) =% @content
        )
        (
          (whitelist icon in? not)
            ("$# - Icon not found: $#" (id icon) =% io.warn)
          when
        )
      if
      content
    ) replace-apply @result
  ) 
) ::

(
  symbol process-content
  (dict :content ==> dict :result)
  (
    content "id" dict.get :id
    content "content-type" dict.get :ct
    "page" :tpl
    (ct "article" ==) ("article" @tpl) when
    (ct "project" ==) ("project" @tpl) when
    (id "index" ==) (content @HOME "home" @tpl) when
    "" :page
    "" :contents
    content input-fread @contents
    (content "ext" dict.get ".md" ==) (contents content markdown @contents) when
    content contents prepend-toc "contents" dict.set @content
    tpl content mustache @page
    id page process-icons @page
    id page process-code @page
    content page "contents" dict.set @content
    (
        ((id "^(glyph|ruby-compendium)(/|\\\\)book" match?) ()) ;;Do not add index.html due to the way book internal links work.
        ((id "index" !=)(
          content 
            "$1/index" (id) =% "id" dict.set 
            ".html" "ext" dict.set 
            ;; ID must be preserved for links on home page.
            id "rawid" dict.set
          @content
        ))
    ) case
    content @result
  )
) ::
;; Process a content file applying the appropriate template
;; and compiling markdown to HTML if necessary.


(
  symbol generate-home
  (==>)
  (
    ;; Sort articles by timestamp
    ARTICLES ("timestamp" dict.get stack.swap "timestamp" dict.get >) sort @ARTICLES
    ARTICLES first        :latest
    ARTICLES 1 5 slice    :recents
    ;; Process summary
    latest input-fread "(?ms)(.+)?<hr" search 1 get latest markdown :summary
    latest summary "summary" dict.set @latest
    latest stack.dup "timestamp" dict.get to-date "date" dict.set @latest
    HOME
    latest "latest" dict.set
    recents "recents" dict.set
    projects-count "projects-count" dict.set
    articles-count "articles-count" dict.set
    grimoire-count "grimoire-count" dict.set
    ;;tags %tags
    stack.dup "home" stack.swap mustache :page
    "home" page process-icons
    "contents" dict.set
    output-fwrite
  )
) ::
;; Process and generate home page


(
  symbol process-css-asset
  (dict :meta ==>)
  (
    "" :contents
    meta (
      (input-fread @contents meta)
      (:temp contents @contents temp)
      (contents "contents" dict.set)
    ) tap
    output-fwrite
  )
) ::
;; Process CSS assets

;;;;; MAIN ;;;;;

true :enable-content-processing

(opts 'no-contents dict.has?)
  (false @enable-content-processing)
when 

(enable-content-processing)
(
  ;; Process all contents
  contents (
    :content
    (((content 'draft dict.has? not) (content 'content-type dict.has?)) &&)
    (
      content "id" dict.get :id
      (content "content-type" dict.get "project" ==) (content PROJECTS append @PROJECTS) when
      (content "content-type" dict.get "spell" ==) (content SPELLS append @SPELLS) when
      content
      projects-count "projects-count" dict.set
      articles-count "articles-count" dict.set
      grimoire-count "grimoire-count" dict.set
      process-timestamp 
      process-content 
      @content 
      (id "index" !=) (content output-fwrite) when
    ) when
  ) foreach

  ;; Create aggregator pages
  create-projects-page
  create-grimoire-page
  create-articles-page

  ;; Generate home page
  generate-home
)
when

;; Process all assets.
assets (
  stack.dup
  (
    (("id" dict.get "^pdf/" match?) (copy-pdf-article-asset))
    (("ext" dict.get ".css" match?) (process-css-asset))
    ((true) (output-cp))
  ) case
) foreach