Revisions
This revisions table has been automatically generated
from the git
history
of this website repository, and the change
descriptions may not always be as useful as they
should.
You can consult the source of this file in its current
version here.
#+END_SRC
#+BEGIN_SRC sass :tangle site/style/plugins.sass
table
border-top : 2px solid black
border-bottom : 2px solid black
border-collapse : collapse
width : 35rem
td
border-bottom : 1px solid black
padding : .5em
#history .commit
font-size : smaller
font-family : 'Fira Code', monospace
width : 7em
text-align : center
#+END_SRC
*** Implementation
We use the built-in [[https://soupault.neocities.org/reference-manual/#widgets-preprocess-element][=preprocess_element=]] to implement, which means we need a
script which gets its input from the standard input, and echoes its output to
the standard input.
#+BEGIN_SRC toml :tangle soupault.conf
[widgets.generate-history]
widget = "preprocess_element"
selector = "#history"
command = 'scripts/history.sh templates/history.html'
action = "replace_content"
#+END_SRC
This plugin proceeds as follows:
1. Using an ad-hoc script, it generates a JSON containing for each revision
- The subject, date, hash, and abbreviated hash of the related commit
- The name of the file at the time of this commit
2. This JSON is passed to a mustache engine (~haskell-mustache~) with a
proper template
3. The content of the selected DOM element is replaced with the output of
~haskell-mustache~
This translates in Bash like this.
#+BEGIN_SRC bash :tangle scripts/history.sh :shebang "#!/usr/bin/bash"
function main () {
local file="${1}"
local template="${2}"
tmp_file=$(mktemp)
generate_json ${file} > ${tmp_file}
haskell-mustache ${template} ${tmp_file}
rm ${tmp_file}
}
#+END_SRC
Generating the expected JSON is therefore as simple as:
- Fetching the logs
- Reading 8 line from the logs, parse the filename from the 6th
line
- Outputing the JSON
We will use ~git~ to get the information we need. By default, ~git~
subcommands use a pager when its output is likely to be long. This
typically includes ~git-log~. To disable this behavior, ~git~ exposes
the ~--no-pager~ command. Besides, we also need ~--follow~ and
~--stat~ to deal with file renaming. Without this option, ~git-log~
stops when the file first appears in the repository, even if this
“creation” is actually a renaming. Therefore, the ~git~ command line
we use to collect our history is
#+NAME: gitlog
#+BEGIN_SRC bash :tangle scripts/history.sh :noweb yes
function gitlog () {
local file="${1}"
git --no-pager log \
--follow \
--stat=10000 \
--pretty=format:'%s%n%h%n%H%n%cs%n' \
"${file}"
}
#+END_SRC
This function will generate a sequence of 8 lines containing all the
relevant information we are looking for, for each commit, namely:
- Subject
- Abbreviated hash
- Full hash
- Date
- Empty line
- Change summary
- Shortlog
- Empty line
For instance, the =gitlog= function will output the following lines
for the last commit of this very file:
#+BEGIN_SRC bash :results verbatim :exports results :noweb yes
<