aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Skec <skec@protonmail.ch>2021-02-27 16:42:54 +1100
committerFelix Queißner <felix@ib-queissner.de>2021-03-02 10:55:50 +0100
commit7644e39d6559733fde244d1fef053f9f56b393ce (patch)
tree603636798ba756ab216471d8782de9d5daee438c
parentdd2d988757f3ff07ee26e93a271238c7c1b3efba (diff)
downloadkristall-7644e39d6559733fde244d1fef053f9f56b393ce.tar.gz
Add awk script for gemtext to man conversion
-rwxr-xr-xdoc/gem-to-man.awk83
-rwxr-xr-xdoc/gen-man.sh139
2 files changed, 127 insertions, 95 deletions
diff --git a/doc/gem-to-man.awk b/doc/gem-to-man.awk
new file mode 100755
index 0000000..dd1f590
--- /dev/null
+++ b/doc/gem-to-man.awk
@@ -0,0 +1,83 @@
+#!/bin/awk -f
+
+BEGIN {
+ preformatted=0
+}
+
+# Empty lines
+/^$/ {
+ print "."
+ next
+}
+
+# Level 2 headings become section headings.
+/^##\s/ {
+ print ".SH"
+ sub(/^##\s/, "")
+ print toupper($0)
+ next
+}
+
+# Level 3 headings become subsection headings.
+/^###\s/ {
+ sub(/^###\s/, "")
+ print ".SS \"" $0 "\""
+ next
+}
+
+# Lists
+/^\*\s/ {
+ print ".IP \\(bu 3"
+ sub(/^\*\s/, "")
+ print
+ next
+}
+
+# Preformatted text
+/^```/ {
+ # We simply place indent macros to make
+ # it stand out a little.
+ if (!preformatted)
+ {
+ print ".RS"
+ preformatted=1
+ }
+ else
+ {
+ preformatted=0
+ print ".RE"
+ }
+ next
+}
+
+# Links
+/^=>/ {
+ # Strips => prefix, and separates URL and link
+ # title.
+ sub(/^=>/, "")
+ url=$1
+ $1=""
+ title=$0
+
+ print ".IP"
+
+ # Print either URL on its own or Title: URL if we
+ # have a title.
+ if (title == "")
+ {
+ print url
+ }
+ else
+ {
+ gsub(/^\s/, "", title)
+ print title ": " url
+ }
+
+ next
+}
+
+# Paragraphs (default)
+{
+ print ".PP"
+ print
+}
diff --git a/doc/gen-man.sh b/doc/gen-man.sh
index 6a48cc8..c8e6fdf 100755
--- a/doc/gen-man.sh
+++ b/doc/gen-man.sh
@@ -17,106 +17,55 @@ man_head="./kristall-head.man"
man_tail="./kristall-tail.man"
man_output="./kristall.1"
gemtext_in="../src/about/help.gemini"
+gemtext_converter="./gem-to-man.awk"
-main() {
- # Make sure we have all the stuff we need
- if [[ ! -f "$man_head" ]]; then
- echo "man page head does not exist."
- exit -1
- fi
- if [[ ! -f "$man_tail" ]]; then
- echo "man page tail does not exist"
- exit -1
- fi
- if [[ -z "$man_output" ]]; then
- echo "No output file"
- exit -1
- fi
- if [[ ! -f "$gemtext_in" ]]; then
- echo "Input gemtext file does not exist"
- exit -1
- fi
+# Make sure we have all the stuff we need
+if [[ ! -f "$man_head" ]]; then
+ echo "man page head does not exist."
+ exit -1
+fi
+if [[ ! -f "$man_tail" ]]; then
+ echo "man page tail does not exist"
+ exit -1
+fi
+if [[ -z "$man_output" ]]; then
+ echo "No output file"
+ exit -1
+fi
+if [[ ! -f "$gemtext_in" ]]; then
+ echo "Input gemtext file does not exist"
+ exit -1
+fi
+if [[ ! -f "$gemtext_converter" ]]; then
+ echo "Gemtext converter script does not exist"
+ exit -1
+fi
- # Write the head to the output file.
- cp "$man_head" "$man_output"
+# Write the head to the output file.
+cp "$man_head" "$man_output"
- # Insert last modified date (use last-modified date of help.gemini)
- last_modified=$(date -r "$gemtext_in" +"%Y-%M-%d")
- sed -i "$man_output" -e 's#\$(DATE)#'"$last_modified"'#g'
+# Insert last modified date (use last-modified date of help.gemini)
+last_modified=$(date -r "$gemtext_in" +"%Y-%M-%d")
+sed -i "$man_output" -e 's#\$(DATE)#'"$last_modified"'#g'
- # Convert gemtext about page to roff's man format:
- convert_gemtext "$gemtext_in" >> "$man_output"
+# Some pre-processing before giving our gemtext to the awk script.
+gem_in=$(
+ # Read input file
+ cat "$gemtext_in" |
- # Write the tail to the output file.
- cat "$man_tail" >> "$man_output"
-}
+ # Strip a few lines from beginning/end of file.
+ tail -n +7 |
+ head -n -7 |
-# Simple converter for gemtext to roff
-# Note that this is not a converter of "standard" gemtext, and is
-# designed specifically for kristall's about page. Thus, this may not work
-# as expected on other gemtext files (may require a little tweaking)
-convert_gemtext() {
- gem_in="$(cat $1 | tail -n +7 | head -n -7)"
+ # First expression replaces all [Text like this] with bold text.
+ # Second expression replaces text like *This* or _this_ with italic text.
+ sed -E \
+ -e 's#\[([^]]*)\]#\\fB\1\\fR#g' \
+ -e 's#(^|[.,!? ]+)[*_]([^*_ ]+[^*_]+[^*_ ]+)[*_]($|[.,!? ])#\1\\fI\2\\fR\3#g'
+)
- # Replace [Text] with bold text.
- gem_in="$(echo "$gem_in" | sed -Ee 's#\[([^]]*)\]#\\fB\1\\fR#g')"
+# Convert gemtext to man format
+echo "$gem_in" | "$gemtext_converter" >> "$man_output"
- # Replace *Text* or _text_ with italic (not bold to help differentiate with the above)
- # (Regex is derived from one in geminirenderer.cpp)
- gem_in="$(echo "$gem_in" | sed -Ee 's#(^|[.,!? ]+)[*_]([^*_ ]+[^*_]+[^*_ ]+)[*_]($|[.,!? ])#\1\\fI\2\\fR\3#g')"
-
- is_preformatted=false
-
- # Iterate over all the lines in the gemtext file
- while IFS= read -r line; do
- if echo "$line" | grep -q '^##\s'; then
- # Top level heading
- text="${line#'## '}"
- echo ".SH"
- echo "${text^^}"
- elif echo "$line" | grep -q '^###\s'; then
- # Level 2 heading
- text="${line#'### '}"
- echo ".SS"
- echo "$text"
- elif echo "$line" | grep -q '^\*\s'; then
- # Lists
- text="${line#'* '}"
- echo ".IP \(bu 3"
- echo "${text}"
- elif echo "$line" | grep -q '^\=>'; then
- # Links. We only show the URL itself if there is one.
- text="${line#'=>'}"
- title="$(echo "$text" | awk '{$1=""; print $0}')"
- text="$(echo "$text" | awk '{print $1}')"
-
- # Prefix with URL title if we have one
- if [[ -n "$title" ]]; then
- text="$title: $text"
- fi
-
- echo ".IP"
-
- # Echo the text, we also strip leading whitespace
- echo "$text" | sed -e 's#^[[:space:]]*##'
- elif echo "$line" | grep -q '^```'; then
- # Preformatted text. We just indent it
- if [[ "$is_preformatted" = false ]]; then
- echo ".RS"
- is_preformatted=true
- else
- echo ".RE"
- is_preformatted=false
- fi
- elif [[ -z "$line" ]]; then
- # Empty lines
- echo "."
- else
- # Regular paragraphs
- echo ".PP"
- echo "$line"
- fi
- done <<< "$gem_in"
-}
-
-main "$@"
+# Write the tail to the output file.
+cat "$man_tail" >> "$man_output"