summaryrefslogtreecommitdiff
path: root/http/bin/gmi2upphtml
blob: 608f4e8bee404c25ad6f68bcfd55661dfddefa09 (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
#!/bin/sh -e

post_title="${1:-}"

is_in_list=0
is_in_link_block=0
is_in_preformatted_block=0
is_in_blockquote=0

if_in_list_then_close()
{
  if [ $is_in_list -ne 0 ]
  then
    echo "</ul>"
    is_in_list=0
  fi
}

if_in_link_block_then_close()
{
  if [ $is_in_link_block -ne 0 ]
  then
    echo "</p>"
    is_in_link_block=0
  fi
}

if_in_preformatted_block_then_close()
{
  if [ $is_in_preformatted_block -ne 0 ]
  then
    echo "</pre>"
    is_in_preformatted_block=0
  fi
}

if_in_blockquote_then_close()
{
  if [ $is_in_blockquote -ne 0 ]
  then
    echo "</blockquote>"
    is_in_blockquote=0
  fi
}

close_all_open_blocks()
{
  if_in_list_then_close
  if_in_link_block_then_close
  if_in_preformatted_block_then_close
  if_in_blockquote_then_close
}

escape_upphtml()
{
  echo "$1" | sed "s/\`/\\\\\`/g" | sed "s/\"/\\\\\"/g" | sed "s/\\\$/\\\\\$/g"
}

close_all_open_blocks_except()
{
  except="$1"
  if [ "$except" != "list" ]
  then
    if_in_list_then_close
  fi
  if [ "$except" != "link" ]
  then
    if_in_link_block_then_close
  fi
  if [ "$except" != "pre" ]
  then
    if_in_preformatted_block_then_close
  fi
  if [ "$except" != "blockquote" ]
  then
    if_in_blockquote_then_close
  fi
}

if [ -z "$post_title" ]
then
  read first_line
  if [ "$(echo "$first_line" | cut -c1-2)" != "# " ]
  then
    echo >&2 "Error: no [post_title] argument provided and no top-level heading at top of the document."
    exit 1
  fi
  post_title="${first_line### }"
fi

echo "#!
export TITLE=\"${post_title} | joeac's blog\"
export DESCRIPTION=\"${post_title} | joeac's blog\"
pp \"\${SHARE}\"/components/meta.upphtml
#!"

while read line
do
  if [ "$(echo $line | sed "s/[[:space:]]//g")" = "" ]
  then
    continue
  fi

  if [ "$(echo $line | cut -c1)" = "#" ]
  then
    title="$(escape_upphtml "${line#*# }")"
    heading="${line%%$title}"
    heading="${heading%% }"
    heading_level="${#heading}"
    if [ "$(echo "$line" | cut -c$(( $heading_level + 1 )) )" = " " ]
    then
      close_all_open_blocks
      echo "<h${heading_level}>${title}</h${heading_level}>"
      continue
    fi
  fi

  if [ "$(echo "$line" | cut -c1-2)" = "=>" ]
  then
    close_all_open_blocks_except link
    href="$(escape_upphtml "$(echo "$line" | sed "s/^=>[[:space:]]*\([[:alnum:][:punct:]]\+\).*$/\1/")")"
    title="$(escape_upphtml "$(echo "$line" | sed "s/^=>[[:space:]]*[[:alnum:][:punct:]]\+[[:space:]]\(.*\)$/\1/")")"
    if [ $is_in_link_block -eq 0 ]
    then
      echo "<p>"
    else
      echo "<br/>"
    fi
    is_in_link_block=1
    echo "=> <a href='${href}'>${title}</a>"
    continue
  fi

  if [ "$( echo "$line" | cut -c1-2)" = "* " ]
  then
    close_all_open_blocks_except list
    if [ $is_in_list -eq 0 ]
    then
      echo "<ul>"
    fi
    is_in_list=1
    echo "<li>$(escape_upphtml "$(echo "$line" | cut -c3-)")</li>"
    continue
  fi

  if [ "$(echo "$line" | cut -c1)" = ">" ]
  then
    close_all_open_blocks_except blockquote
    if [ $is_in_blockquote -eq 0 ]
    then
      echo "<blockquote>"
    fi
    is_in_blockquote=1
    echo "<p>$(escape_upphtml "$(echo "$line" | cut -c2-)")</p>"
    continue
  fi

  if [ "$(echo "$line" | cut -c1-3)" = "\`\`\`" ]
  then
    close_all_open_blocks_except pre
    if [ $is_in_preformatted_block -eq 0 ]
    then
      echo "<pre>"
    else
      echo -e "\n"
    fi
    is_in_preformatted_block=1
    echo "$(escape_upphtml "$(echo "$line" | cut -c4-)")"
    continue
  fi

  close_all_open_blocks
  echo "<p>$(escape_upphtml "${line}")</p>"
done

close_all_open_blocks