Mike Gerwitz

Free Software Hacker+Activist

aboutsummaryrefslogtreecommitdiffstats
blob: 1137b29a64e0a37d154325d4d8a2bb389cd6fbaa (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
#!/bin/bash
# Generate papers HTML page
#
#  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/>.
#
# Papers are (at least at present) written in LaTeX, whereas articles are
# simply posts.  Both are specified in $PAPERFILE.  This page generates
# abstracts for both formats, along with links to each of their output
# formats (one or more of PDF, DVI, HTML).
##

set -euo pipefail

# Refile containing paper references and metadata.
declare -r PAPERFILE=${PAPERFILE:-src/papers.rec}


# List ids of all papers in $PAPERFILE.
paper-list()
{
  recsel -CP id "$PAPERFILE"
}


# Retrieve field FIELD from paper ID in $PAPERFILE.
paper-field()
{
  local -r id=${1?Missing paper id}
  local -r field=${2?Missing paper field}

  recsel -P "$field" -e "id = '$id'" "$PAPERFILE"
}


# Read field FIELD from post metadata recfile FILE.
post-field()
{
  local -r ref=${1?Missing post name}
  local -r field=${2?Missing field name}

  recsel -P "$field" "post/$ref.meta"
}


# Generate abstract for article or paper ID.  Delegates to one of
# {post,latex}-abstract based on its type.
abstract()
{
  local -r id=${1?Missing paper id}

  local type ref
  type=$( paper-field "$id" type )
  ref=$( paper-field "$id" ref )

  case "$type" in
    post|latex)
      "$type-abstract" "$id" "$ref";;
    *)
      echo "Unknown paper type for id \`$id" >&2
      return 1
  esac
}



# Generate abstract for post REF.
post-abstract()
{
  local -r ref=${2?Missing post ref}

  local id title date abstract slug
  id=$( post-field "$ref" id )
  title=$( post-field "$ref" subject )
  date=$( post-field "$ref" date )
  abstract=$( post-field "$ref" abstract )
  slug=$( post-field "$ref" slug )

  cat <<EOF
<article class="abstract paper">
  <h2 class="title" id="$id"><a href="/$slug">$title</a></h2>

  <ul class="links">
    <li class="title">Formats:</li>
    <li><a href="/$slug">View HTML</a></li>
  </ul>

  $abstract

  <p class="date">Posted on $date.</p>
</article>
EOF
}


# Extract title from LaTeX document.  Note that this performs no actual
# processing on that title, so this will need to be e.g. run through Pandoc
# in the future if titles contain something that should be parsed (like
# dashes).
latex-title()
{
  head -n1 | sed '1s/^% //;1a\\'
}


# Produce text of LaTeX abstract (from its abstract.tex).
#
# Two minor transformations are made: Footnotes are removed by exploiting
# Pandoc's behavior of ignoring unknown/unsupported commands, since that
# doesn't look very good in the abstract output.  Emdashes have whitespace
# on either side removed to translate to my modern convention (this can be
# removed when old papers are updated).
latex-abstract-text()
{
  sed 's/\\footnote/\\void/;
       s/ \+--- \+/---/g' \
    | pandoc -flatex -thtml
}


# Generate abstract for LaTeX document (from abstract.tex) ID located at
# path REF.  REF is expected to contain `abstract.tex' and `REF.tex', along
# with the built `REF.pdf' and `REF.dvi'.
latex-abstract()
{
  local -r id=${1?Missing paper id}
  local -r ref=${2?Missing paper ref}

  local -r abstract_tex="$ref/abstract.tex"
  local -r main="$ref/${ref##*/}.tex"
  local -r sans=${main%/*.tex}

  local title abstract pubdate
  title=$( latex-title < "$main" )
  abstract=$( latex-abstract-text < "$abstract_tex" )
  pubdate=$( paper-field "$id" pubdate )

  cat <<EOF
<article class="abstract paper">
  <h2 class="title" id="$id"><a href="/$sans.pdf">$title</a></h2>

  <ul class="links">
    <li class="title">Formats:</li>
    <li><a href="/$sans.pdf">View PDF</a></li>
    <li><a href="/$sans.dvi">View DVI</a></li>
  </ul>

  $abstract

  <p class="date">Published on $pubdate.</p>
</article>
EOF
}


# Generate papers page.
main()
{
  src/mkheader papers Papers

  local papers
  papers=$( recsel -P id src/papers.rec )

  echo '<h1>Papers / Articles</h1>'
  paper-list | while read id; do abstract "$id"; done

  cat src/footer.tpl.htm
}


main "$@"