Mike Gerwitz

Activist for User Freedom

aboutsummaryrefslogtreecommitdiffstats
blob: 8fd821fca3a38412a502de2b112e8cc7e4bd4b23 (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
#!/bin/bash
# Generate HTML from post Markdown source
#
#  Copyright (C) 2019 Mike Gerwitz
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# This script accepts the file name rather than data on stdin because the
# filename encodes the post date.
#
# Note that the `pagetitle' is set to "ignoreme"---it is not used, but is
# needed to suppress the warning pandoc produces without suppressing all
# warnings.
#
# Pandoc is used to generate the HTML and includes a (mostly) static header
# and footer.  Note that this duplicates the date logic in `post2meta',
# because that must be run on this output, but the post must also contain
# the date, and we want to do all HTML processing now.
##

declare -r srcref=/projects/thoughts/tree

set -euo pipefail

# Pandoc output format and extensions.
declare -ra ext=(
  markdown
  smart
  footnotes
  gfm_auto_identifiers
  fancy_lists
  startnum
  tex_math_dollars
)


# Convert extensions to `+'-delimited string.
pexts()
{
  local IFS=+
  echo "${ext[*]}"
}


# Wrap h1 in an hgroup along with the post date.
#
# Sometimes this script is used on things that aren't posts (e.g. normal
# pages), in which case a date will be unavailable and the output will be
# unchanged.
hgroup-wrap()
{
  local -r date=${1?Missing date}
  local -r file=${2?Missing file}

  # Abort if this is not a date prefix
  [[ $date =~ [0-9]{4}-[0-9]{2}-[0-9]{2} ]] || {
    cat
    return
  }

  local -r repo_href="$srcref/$file"

  local anchor
  printf -vanchor '<a class="muted" href="%s">%s</a>' \
         "$repo_href" \
         "$date"

  sed '/^<h1/{
         i<hgroup>
         a<h2 class="date">'"$anchor"'</h2></hgroup>
       }'
}


# Pre-format Markdown files before they get to Pandoc
#
# These may be able to be implemented as Pandoc filters, but I haven't had
# the time to research that yet.  This is actually a fitting real-world
# demonstration of incremental development / MVP that I'm writing about at
# the time that this comment was written (MyCustomBB Part I)!
prefmt()
{
  awk '
    triml {
      gsub( /^ +/, "" )
      triml = 0
    }

    # ties
    { $0 = gensub( /([^\\])~/, "\\1 ", "g" ) }
    { $0 = gensub( /\\~/, "~", "g" ) }

    # TeX-style newline removal
    /%$/ {
      gsub( /%$/, "" )
      printf "%s", $0
      triml = 1
      next
    }

    { print }
  '
}


# Generate HTML from post.  Note that `pagetitle' is set just to suppress
# Pandoc warnings about it missing; it is unused.
main()
{
  local -r file=${1?Missing file name}
  local -r base=$( basename "$file" .md )
  local -r date=${base:0:10}

  pandoc -f"$( pexts )" -thtml5 \
         --standalone --template src/pandoc.tpl \
         --metadata pagetitle:ignoreme \
         --base-header-level=1 \
         -B <( src/mkheader post @__PAGE_TITLE__@ ) \
         -A src/footer.tpl.htm \
         < <( prefmt < "$file" ) \
    | src/h12title @__PAGE_TITLE__@ \
    | hgroup-wrap "$date" "$file"
}


main "$@"