fr: script to reformat the HTML pages (remove indentation, single out paragraphs...
authorThérèse Godefroy <godef.th@free.fr>
Wed, 23 Jul 2014 16:31:28 +0000 (18:31 +0200)
committerThérèse Godefroy <godef.th@free.fr>
Wed, 23 Jul 2014 16:31:28 +0000 (18:31 +0200)
fr/kitchen/page-reformat [new file with mode: 0755]

diff --git a/fr/kitchen/page-reformat b/fr/kitchen/page-reformat
new file mode 100755 (executable)
index 0000000..cb2caf9
--- /dev/null
@@ -0,0 +1,104 @@
+#!/bin/bash
+
+# page-reformat: reformat the original pages of emailselfdefense.fsf.org
+
+# cd to the directory where the script is located, then:
+#      $ ./page-reformat [path to the HTML file]
+# You can also drag-and-drop the file into the terminal.
+# The result has extension .html.html to avoid writing over the original page.
+
+# If you want to wrap the text, uncomment line 97 and comment out line 94.
+
+
+cp $1 tmp
+
+# Remove javascript, which shouldn't be reformated.
+sed -i '/jquery-1.11.0.min.js/,$d' tmp
+
+# Remove leading and trailing spaces/tabs.
+sed -i 's,\t, ,g' tmp
+sed -i 's,^ *,,' tmp
+sed -i 's, *$,,' tmp
+
+# Remove LF after </a>.
+sed -i '/<\/a>$/ {N; s,<\/a>\n\([^<]\),<\/a>\1,}' tmp
+
+# One string per paragraph, header or list item.
+for tag in li p strong a h3; do
+  sed -i "/<$tag[^>]*>$/ {N; s,\\n, ,}" tmp
+done
+for tag in a strong; do
+  sed -i "/<\\/$tag>$/ {N; s,\\n, ,}" tmp
+done
+# This command may need to be repeated. Adjust the number of repeats. This
+# could be done by looping back to a sed marker, but a while loop seems
+# quicker.
+i=0
+while (( i < 2 )); do
+  sed -i '/[^<>]$/ {N; s,\([^<>]\)\n,\1 ,}' tmp
+  let i=i+1
+done
+
+sed -i '/ \/>$/ {N; s,\( \/>\)\n,\1 ,}' tmp
+sed -i '/ <a[^>]*>$/ {N; s,\(<a[^>]*>\)\n\([^<]\),\1 \2,}' tmp
+
+# Make sure there is only one paragraph per string. This command may need to
+# be repeated. Adjust the number of repeats.
+i=0
+while (( i < 2 )); do
+  sed -i 's,</p>\(.\+\)$,</p>\n\1,' tmp
+  let i=i+1
+done
+
+# Single out the tags which include p (will also work for pre).
+sed -i 's,\(.\)<p,\1\n<p,' tmp
+
+# Single-out input meta and link.
+for tag in input meta link link; do
+  sed -i "s,> <$tag,>\n<$tag," tmp
+done
+# Remove leading and trailing spaces, double spaces and blank lines.
+sed -i 's,^ *,,' tmp
+sed -i 's, *$,,' tmp
+sed -i 's,  , ,g' tmp
+sed -i '/^$/d' tmp
+
+# Fuse comment with </p>.
+sed -i '/<\/p>$/ {N;s,\n\(<!-- [^~]\),\1,}' tmp
+
+# Separate truncated "~~~" comment from fused tag.
+sed -i 's,~~~[ ]\?[-]\?[-]\?[ ]\?<,~~~\n<,' tmp
+
+# Fuse header, section and footer with the corresponding div.
+for tag in header section footer; do
+  sed -i "/^<$tag/ {N; s,\\(<$tag[^>]*>\\)\\n<div>,\\1<div>,}" tmp
+  sed -i "/^<\\/div>$/ {N; s,<\\/div>\\n\\(<\\/$tag>\\),</div>\\1,}" tmp
+done
+
+# Add LF before main sections and commented-out parts.
+sed -i 's,<!-- ~~,\n<!-- ~~,' tmp
+sed -i '/COMMENTED OUT/ s,^,\n,' tmp
+
+# Make the text more readable.
+for tag in p h1 h2 h3 h4 dl title form; do
+  sed -i "s,<$tag,\\n&," tmp
+done
+for tag in p dl ul h1 h2 h3 h4 title head footer form script; do
+  sed -i "/<\\/$tag>/s,$,\\n," tmp
+done
+sed -i '/<\/dd>/ {N; s,</dd>\n<dt,</dd>\n\n<dt,}' tmp
+sed -i '/<\/dt>/ {N; s,</dt>\n<dd,</dt>\n\n<dd,}' tmp
+sed -i 's,</p></span>$,</p>\n</span>,' tmp
+
+sed -i 's, alt=,\nalt=,g' tmp
+sed -i 's, | , |\n,g' tmp
+mv tmp $1.html
+
+# Wrap the text.
+#fmt -s -w 95 tmp > $1.html
+
+# Remove extra LFs, if any.
+sed -i ':a /^$/ {N; s,\n$,,; ba}' $1.html
+sed -i ':a /^\n*$/ {$d; N; ba}' $1.html
+
+rm -f tmp