Permalink
Browse files

All the code changes that correspond to the HTML changes in the previ…

…ous commit; plus some changes to pick up table border presentation markup from Word. These changes are not yet complete.
  • Loading branch information...
1 parent 2835033 commit f46b6a3e8e6cdd79c28ad19645964d7d02985f81 @jorendorff jorendorff committed Mar 27, 2012
Showing with 317 additions and 222 deletions.
  1. +33 −6 docx.py
  2. +13 −4 es-spec.py
  3. +182 −182 es5.1-final.html
  4. +75 −24 fixups.py
  5. +14 −6 transform.py
View
39 docx.py
@@ -40,6 +40,13 @@ def bloat(name):
k_left = bloat('left')
k_hanging = bloat('hanging')
+
+def parse_color(s):
+ if s is not None and re.match(r'^[0-9a-fA-F]{6}$', s):
+ return '#' + s
+ else:
+ return None
+
def parse_pr(e):
font_keys = {k_ascii, k_hAnsi, k_cs, k_eastAsia, bloat('hint')}
@@ -101,22 +108,41 @@ def put(k, v):
elif name == 'shd':
val = k.get(k_val)
if val == 'solid':
- color = k.get(k_color)
+ color = parse_color(k.get(k_color))
elif val == 'clear':
- color = k.get(k_fill)
+ color = parse_color(k.get(k_fill))
else:
color = None
-
- if color is not None and re.match(r'^[0-9a-fA-F]{6}$', color):
- put('background-color', '#' + color)
+
+
+ if color is not None:
+ put('background-color', color)
+
+ elif name in ('tcBorders', 'tblBorders'):
+ # tblBorders can have insideH/insideV elements that are applied to
+ # all horizontal/vertical borders between cells in the table. For
+ # now, we store that style information in CSS properties named
+ # -ooxml-border-insideH/insideV; later we will turn that into
+ # border-top/left properties on all the individual table cells.
+ for side in ('top', 'bottom', 'left', 'right', 'insideH', 'insideV'):
+ for side_style in k.findall(bloat(side)):
+ if side_style.get(k_val) == 'single':
+ color = parse_color(side_style.get(k_color)) or 'black'
+ sz = side_style.get(k_sz)
+ if sz is not None:
+ sz = int(sz) // 6
+ prop = 'border-' + side
+ if side.startswith('inside'):
+ prop = '@' + prop
+ put('border-' + side, '{}px solid {}'.format(sz, color))
elif name == 'sz':
if list(k.keys()) == [k_val]:
# The unit of w:sz is half-points lol.
v = float(k.get(k_val)) / 2
#put('font-size', str(v) + 'pt')
- # todo: jc, ind, spacing, contextualSpacing
+ # todo: jc, spacing, contextualSpacing
# todo: pBdr
elif name == 'ind':
@@ -230,6 +256,7 @@ def populate_full_style(s):
k_pStyle = bloat('pStyle')
k_numFmt = bloat('numFmt')
k_numStyleLink = bloat('numStyleLink')
+k_sz = bloat('sz')
class Num:
def __init__(self, abstract_num_id, overrides):
View
@@ -1,5 +1,6 @@
#!/usr/bin/env python3.2
+import sys, os
import docx
import htmodel
from transform import transform
@@ -38,11 +39,19 @@ def save_html(docxfile, filename):
htmodel.save_html(filename, result)
-es6_draft = docx.load("es6-draft.docx")
+if len(sys.argv) != 2:
+ print("usage: {} filename.docx".format(sys.argv[0]), file=sys.stderr)
+ sys.exit(1)
+
+in_filename = sys.argv[1]
+base, ext = os.path.splitext(in_filename)
+assert ext in ('.docx', '.dotx')
+out_filename = base + '.html'
+doc = docx.load(in_filename)
+save_html(doc, out_filename)
+
+# Some other things that can be done with a docx.Document:
#sketch_schema(es6_draft.document)
#es6_draft._extract()
#es6_draft._dump_styles()
-save_html(es6_draft, 'es6-draft.html')
-#es51 = docx.load("es5.1-final.dotx")
-#save_html(es51, "es5.1-final.html")
Oops, something went wrong.

0 comments on commit f46b6a3

Please sign in to comment.