Skip to content

Commit

Permalink
Fix chapter links when exporting XML.
Browse files Browse the repository at this point in the history
  • Loading branch information
munificent committed Jun 19, 2014
1 parent 604b1e7 commit e13773d
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 5 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ code/flyweight/tiles/Build/
.sass-cache/
xcuserdata/
*.xcuserstate
project.xcworkspace/
project.xcworkspace/

xml/
16 changes: 16 additions & 0 deletions asset/template.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" standalone="yes" ?>
<!DOCTYPE chapter [
<!ENTITY mdash "&#8212;"> <!-- em dash, U+2014 ISOpub -->
<!ENTITY lsquo "&#8216;"> <!-- left single quotation mark,
U+2018 ISOnum -->
<!ENTITY rsquo "&#8217;"> <!-- right single quotation mark,
U+2019 ISOnum -->
<!ENTITY sbquo "&#8218;"> <!-- single low-9 quotation mark, U+201A NEW -->
<!ENTITY ldquo "&#8220;"> <!-- left double quotation mark,
U+201C ISOnum -->
<!ENTITY rdquo "&#8221;"> <!-- right double quotation mark,
U+201D ISOnum -->
]>
<chapter>
{{body}}
</chapter>
19 changes: 19 additions & 0 deletions note/print workflow.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
- Export to XML
- Import XML
- Place it in main text flow
- Untag everything and remove the story structure
- Pull out asides and restyle
- Change first paragraph body text styles
- Set chapter title and number on first page
- Update section in running footer
- Clean up
- Make sure there aren't bits of 14.4pt line height
- Fix prose references to hyperlinks to make them make sense
- Add illustrations
- Make 600 dpi bitmap
- Save to Print/Illustrations/...
- Place
- Make inline anchor
- Set Position to "Above Line" with 9pts of space before and 4.5pts after
- Add a caption
- Position asides
48 changes: 44 additions & 4 deletions script/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,32 @@
"Spatial Partition"
]

# URLs for hyperlinks to chapters. Note that the order is significant here.
# The index in this list + 1 is the chapter's number in the table of contents.
CHAPTER_HREFS = [
"introduction.html",
"architecture-performance-and-games.html",
"command.html",
"flyweight.html",
"observer.html",
"prototype.html",
"singleton.html",
"state.html",
"double-buffer.html",
"game-loop.html",
"update-method.html",
"bytecode.html",
"subclass-sandbox.html",
"type-object.html",
"component.html",
"event-queue.html",
"service-locator.html",
"data-locality.html",
"dirty-flag.html",
"object-pool.html",
"spatial-partition.html"
]

num_chapters = 0
empty_chapters = 0
total_words = 0
Expand Down Expand Up @@ -121,7 +147,7 @@ def format_file(path, nav, skip_up_to_date):
else:
print "UNKNOWN COMMAND:", command, args

elif stripped.startswith('#'):
elif extension != "xml" and stripped.startswith('#'):
# Build the page navigation from the headers.
index = stripped.find(" ")
headertype = stripped[:index]
Expand Down Expand Up @@ -230,10 +256,24 @@ def clean_up_code_xml(code):

return code

def fix_link(match):
tag = match.group(1)
contents = match.group(2)
href = re.search(r'href\s*=\s*"([^"]+)"', tag).group(1)

# If it's not a link to a chapter, just return the contents of the link and
# strip out the link itself.
if not href in CHAPTER_HREFS:
return contents

# Turn it into a chapter number reference.
return "{}<chap-ref> ({})</chap-ref>".format(
contents, CHAPTER_HREFS.index(href) + 1)

def clean_up_xhtml(html):
# Remove links.
html = re.sub(r"<a\s[^>]+>", "", html)
html = re.sub(r"</a>", "", html)
# Replace chapter links with chapter number references and remove other
# links.
html = re.sub(r"<a\s+([^>]+)>([^<]+)</a>", fix_link, html)

# Ditch newlines in the middle of blocks of text. Out of sheer malice,
# even though they are meaningless in actual XML, InDesign treats them
Expand Down

0 comments on commit e13773d

Please sign in to comment.