aboutsummaryrefslogtreecommitdiff
blob: 6ed4c1989b4c018a31c7b95f9ef28e54621f000a (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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
From 88f5739d3f0d34c51f318fc460b843253b4242e0 Mon Sep 17 00:00:00 2001
From: Nicolas Bock <nicolasbock@gmail.com>
Date: Fri, 8 Nov 2013 09:58:55 -0700
Subject: [PATCH 2/2] Make markupSanitizer.py support python 3.1 and 3.2

The script only supports <python-3 because of how uni-code literals are
treated in python-3.{1,2}. In python-2, a unicode string had to be prefixed
with 'u', while this notation was dropped in python-3.{1,2}. I have added a
check to the script so that it runs now with python-2.7 and python-3.{1,2,3}.
---
 doc/markupSanitizer.py | 179 ++++++++++++++++++++++++++-----------------------
 1 file changed, 95 insertions(+), 84 deletions(-)

diff --git a/doc/markupSanitizer.py b/doc/markupSanitizer.py
index f206cab..6fe247d 100755
--- a/doc/markupSanitizer.py
+++ b/doc/markupSanitizer.py
@@ -4,87 +4,98 @@ from bs4 import BeautifulSoup
 import sys
 import os
 
-# Accept filename as user input
-argc = len( sys.argv )
-if (argc < 2): raise Exception
-fileName = sys.argv[1];
-
-# Construct a DOM object
-soup = BeautifulSoup(open(fileName), "lxml")
-
-# Assuming, tt tags are not spewed recklessly by latex2html,
-# replace them with code tags
-for t in soup('tt'):
-    t.wrap( soup.new_tag('code') )
-    t.unwrap()
-
-# Rewrap all div class=alltt blocks in pre tags
-for d in soup('div','alltt'):
-    d.wrap( soup.new_tag('pre') )
-    d.unwrap()
-
-# Remove br and span tags from within pre sections
-for p in soup('pre'):
-    for b in p('br'):
-        b.extract()
-    for s in p('span'):
-        s.unwrap()
-
-# Remove all useless class 'arabic' spans
-for s in soup('span','arabic'):
-    s.unwrap()
-
-# Extract the navigation bar
-navmenu = soup.find('div', 'navigation')
-if navmenu:
-    navmenu.extract()
-
-# Wrap the remaining contents within a div
-if not soup.find('div', id='maincontainer'):
-    soup.body['id'] = 'maincontainer'
-    soup.body.name = 'div'
-    soup.find('div', id='maincontainer').wrap( soup.new_tag('body') )
-
-if navmenu:
-    # If this navmenu doesn't already have a TOC, insert one
-    if not navmenu.find('ul','manual-toc'):
-        # Add a toc within the navmenu
-        navmenuTOC = BeautifulSoup(open("tmp-navmenu.html"), "lxml")
-        navmenuTOC = navmenuTOC.find('ul','manual-toc').extract()
-        navmenuTOC.append( BeautifulSoup("".join([
-        '<li><a href="http://charm.cs.illinois.edu">PPL Homepage</a></li>',
-        '<li><a href="http://charm.cs.illinois.edu/help">Other Manuals</a></li>'])
-        ) )
-        navmenu.append(navmenuTOC)
-
-    # Insert navigation symbols to prev and next links
-    prevsymbol = soup.new_tag('span')
-    prevsymbol['class'] = 'navsymbol'
-    prevsymbol.string = u'\xab'
-    prv = navmenu.find('li',id='nav-prev')
-    if prv:
-        prv.find('a').insert(0, prevsymbol)
-
-    nextsymbol = soup.new_tag('span')
-    nextsymbol['class'] = 'navsymbol'
-    nextsymbol.string = u'\xbb'
-    nxt = navmenu.find('li',id='nav-next')
-    if nxt:
-        nxt.find('a').append(nextsymbol)
-
-    # Reinsert the navigation bar at the end
-    soup.body.append(navmenu)
-
-# Extract the title
-titl = soup.find('title')
-
-# Replace the head section with the user-supplied head markup
-soup.find('head').extract()
-newhead = BeautifulSoup(open("../assets/head.html"), "lxml")
-newhead = newhead.find('head').extract()
-newhead.append(titl)
-soup.html.body.insert_before(newhead)
-
-# Print cleaned up markup to stdout
-print( soup.prettify(formatter="html") )
-
+def main ():
+  # Accept filename as user input
+  argc = len( sys.argv )
+  if (argc < 2): raise Exception
+  fileName = sys.argv[1];
+
+  # Construct a DOM object
+  soup = BeautifulSoup(open(fileName), "lxml")
+
+  # Assuming, tt tags are not spewed recklessly by latex2html,
+  # replace them with code tags
+  for t in soup('tt'):
+      t.wrap( soup.new_tag('code') )
+      t.unwrap()
+
+  # Rewrap all div class=alltt blocks in pre tags
+  for d in soup('div','alltt'):
+      d.wrap( soup.new_tag('pre') )
+      d.unwrap()
+
+  # Remove br and span tags from within pre sections
+  for p in soup('pre'):
+      for b in p('br'):
+          b.extract()
+      for s in p('span'):
+          s.unwrap()
+
+  # Remove all useless class 'arabic' spans
+  for s in soup('span','arabic'):
+      s.unwrap()
+
+  # Extract the navigation bar
+  navmenu = soup.find('div', 'navigation')
+  if navmenu:
+      navmenu.extract()
+
+  # Wrap the remaining contents within a div
+  if not soup.find('div', id='maincontainer'):
+      soup.body['id'] = 'maincontainer'
+      soup.body.name = 'div'
+      soup.find('div', id='maincontainer').wrap( soup.new_tag('body') )
+
+  if navmenu:
+      # If this navmenu doesn't already have a TOC, insert one
+      if not navmenu.find('ul','manual-toc'):
+          # Add a toc within the navmenu
+          navmenuTOC = BeautifulSoup(open("tmp-navmenu.html"), "lxml")
+          navmenuTOC = navmenuTOC.find('ul','manual-toc').extract()
+          navmenuTOC.append( BeautifulSoup("".join([
+          '<li><a href="http://charm.cs.illinois.edu">PPL Homepage</a></li>',
+          '<li><a href="http://charm.cs.illinois.edu/help">Other Manuals</a></li>'])
+          ) )
+          navmenu.append(navmenuTOC)
+
+      # Insert navigation symbols to prev and next links
+      prevsymbol = soup.new_tag('span')
+      prevsymbol['class'] = 'navsymbol'
+      prevsymbol.string = u('\xab')
+      prv = navmenu.find('li',id='nav-prev')
+      if prv:
+          prv.find('a').insert(0, prevsymbol)
+
+      nextsymbol = soup.new_tag('span')
+      nextsymbol['class'] = 'navsymbol'
+      nextsymbol.string = u('\xbb')
+      nxt = navmenu.find('li',id='nav-next')
+      if nxt:
+          nxt.find('a').append(nextsymbol)
+
+      # Reinsert the navigation bar at the end
+      soup.body.append(navmenu)
+
+  # Extract the title
+  titl = soup.find('title')
+
+  # Replace the head section with the user-supplied head markup
+  soup.find('head').extract()
+  newhead = BeautifulSoup(open("../assets/head.html"), "lxml")
+  newhead = newhead.find('head').extract()
+  newhead.append(titl)
+  soup.html.body.insert_before(newhead)
+
+  # Print cleaned up markup to stdout
+  print( soup.prettify(formatter="html") )
+
+if sys.version < '3':
+  import codecs
+  def u (x):
+    return codecs.unicode_escape_decode(x)[0]
+else:
+  def u (x):
+    return x
+
+if __name__ == "__main__":
+  main()
-- 
1.8.1.5