fr: script to visualize differences between 2 pages.
[enc.git] / fr / kitchen / page-reformat
CommitLineData
ddf5b37e
TG
1#!/bin/bash
2
3# page-reformat: reformat the original pages of emailselfdefense.fsf.org
4
5# cd to the directory where the script is located, then:
6# $ ./page-reformat [path to the HTML file]
7# You can also drag-and-drop the file into the terminal.
8# The result has extension .html.html to avoid writing over the original page.
9
10# If you want to wrap the text, uncomment line 97 and comment out line 94.
11
12
13cp $1 tmp
14
15# Remove javascript, which shouldn't be reformated.
16sed -i '/jquery-1.11.0.min.js/,$d' tmp
17
18# Remove leading and trailing spaces/tabs.
19sed -i 's,\t, ,g' tmp
20sed -i 's,^ *,,' tmp
21sed -i 's, *$,,' tmp
22
23# Remove LF after </a>.
24sed -i '/<\/a>$/ {N; s,<\/a>\n\([^<]\),<\/a>\1,}' tmp
25
26# One string per paragraph, header or list item.
27for tag in li p strong a h3; do
28 sed -i "/<$tag[^>]*>$/ {N; s,\\n, ,}" tmp
29done
30for tag in a strong; do
31 sed -i "/<\\/$tag>$/ {N; s,\\n, ,}" tmp
32done
33# This command may need to be repeated. Adjust the number of repeats. This
34# could be done by looping back to a sed marker, but a while loop seems
35# quicker.
36i=0
37while (( i < 2 )); do
38 sed -i '/[^<>]$/ {N; s,\([^<>]\)\n,\1 ,}' tmp
39 let i=i+1
40done
41
42sed -i '/ \/>$/ {N; s,\( \/>\)\n,\1 ,}' tmp
43sed -i '/ <a[^>]*>$/ {N; s,\(<a[^>]*>\)\n\([^<]\),\1 \2,}' tmp
44
45# Make sure there is only one paragraph per string. This command may need to
46# be repeated. Adjust the number of repeats.
47i=0
48while (( i < 2 )); do
49 sed -i 's,</p>\(.\+\)$,</p>\n\1,' tmp
50 let i=i+1
51done
52
53# Single out the tags which include p (will also work for pre).
54sed -i 's,\(.\)<p,\1\n<p,' tmp
55
56# Single-out input meta and link.
57for tag in input meta link link; do
58 sed -i "s,> <$tag,>\n<$tag," tmp
59done
60# Remove leading and trailing spaces, double spaces and blank lines.
61sed -i 's,^ *,,' tmp
62sed -i 's, *$,,' tmp
63sed -i 's, , ,g' tmp
64sed -i '/^$/d' tmp
65
66# Fuse comment with </p>.
67sed -i '/<\/p>$/ {N;s,\n\(<!-- [^~]\),\1,}' tmp
68
69# Separate truncated "~~~" comment from fused tag.
70sed -i 's,~~~[ ]\?[-]\?[-]\?[ ]\?<,~~~\n<,' tmp
71
72# Fuse header, section and footer with the corresponding div.
73for tag in header section footer; do
74 sed -i "/^<$tag/ {N; s,\\(<$tag[^>]*>\\)\\n<div>,\\1<div>,}" tmp
75 sed -i "/^<\\/div>$/ {N; s,<\\/div>\\n\\(<\\/$tag>\\),</div>\\1,}" tmp
76done
77
78# Add LF before main sections and commented-out parts.
79sed -i 's,<!-- ~~,\n<!-- ~~,' tmp
80sed -i '/COMMENTED OUT/ s,^,\n,' tmp
81
82# Make the text more readable.
83for tag in p h1 h2 h3 h4 dl title form; do
84 sed -i "s,<$tag,\\n&," tmp
85done
86for tag in p dl ul h1 h2 h3 h4 title head footer form script; do
87 sed -i "/<\\/$tag>/s,$,\\n," tmp
88done
89sed -i '/<\/dd>/ {N; s,</dd>\n<dt,</dd>\n\n<dt,}' tmp
90sed -i '/<\/dt>/ {N; s,</dt>\n<dd,</dt>\n\n<dd,}' tmp
91sed -i 's,</p></span>$,</p>\n</span>,' tmp
92
93sed -i 's, alt=,\nalt=,g' tmp
94sed -i 's, | , |\n,g' tmp
95mv tmp $1.html
96
97# Wrap the text.
98#fmt -s -w 95 tmp > $1.html
99
100# Remove extra LFs, if any.
101sed -i ':a /^$/ {N; s,\n$,,; ba}' $1.html
102sed -i ':a /^\n*$/ {$d; N; ba}' $1.html
103
104rm -f tmp