Permalink
Browse files

[svn r4318] r5421@lenny: sbehnel | 2010-01-27 18:10:35 +0100

 fix PDF building for docutils 0.6

--HG--
branch : trunk
  • Loading branch information...
scoder
scoder committed Jan 27, 2010
1 parent 7ba3c72 commit 6d2a084d7d87c7db6ac34a2a97ebcf661ae34075
Showing with 40 additions and 18 deletions.
  1. +40 −18 doc/mklatex.py
View
@@ -43,8 +43,25 @@
DOCUMENT_CLASS = r"""
\documentclass[10pt,english]{report}
\usepackage[a4paper]{geometry}
+\usepackage{tabularx}
+\usepackage{ifthen}
+\usepackage[pdftex]{graphicx}
\parindent0pt
\parskip1ex
+
+%%% Fallback definitions for Docutils-specific commands
+
+% providelength (provide a length variable and set default, if it is new)
+\providecommand*{\DUprovidelength}[2]{
+ \ifthenelse{\isundefined{#1}}{\newlength{#1}\setlength{#1}{#2}}{}
+}
+
+% docinfo (width of docinfo table)
+\DUprovidelength{\DUdocinfowidth}{0.9\textwidth}
+
+% titlereference role
+\providecommand*{\DUroletitlereference}[1]{\textsl{#1}}
+
"""
PYGMENTS_IMPORT = r"""
@@ -86,7 +103,10 @@ def copy_epydoc_macros(src, dest, existing_header_lines):
doc = file(src, 'r')
out = file(dest, "w")
for line in doc:
- if line.startswith('%% generator') or line.startswith('% generated by '):
+ if line.startswith('%% generator') \
+ or line.startswith('% generated by ') \
+ or '\\begin{document}' in line \
+ or '\\makeindex' in line:
break
if line.startswith('%') or \
r'\documentclass' in line or \
@@ -107,11 +127,11 @@ def noop(input):
counter_no = 0
-def tex_postprocess(src, dest, want_header = False, process_line=noop):
+def tex_postprocess(src_path, dest_path, want_header=False, process_line=noop):
"""
Postprocessing of the LaTeX file generated from ReST.
- Reads file src and saves to dest only the true content
+ Reads file src_path and saves to dest_path only the true content
(without the document header and final) - so it is suitable
to be used as part of the longer document.
@@ -127,22 +147,27 @@ def tex_postprocess(src, dest, want_header = False, process_line=noop):
counter_no = counter_no + 1
counter_text = "listcnt%d" % counter_no
- search_title = re.compile(r'\\title{([^}]*)}').search
- skipping = re.compile(r'(\\end{document}|\\tableofcontents)').search
+ search_title = re.compile(r'\\title{([^{}]*(?:{[^}]*})*)}').search
+ skipping = re.compile(r'(\\end{document}|\\tableofcontents|^%)').search
+
+ src = file(src_path)
+ dest = file(dest_path, "w")
- src = file(src)
- dest = file(dest, "w")
+ src_text = src.read()
+ src.close()
- iter_lines = iter(src.readlines())
+ title = search_title(src_text)
+ if title:
+ # remove any commands from the title
+ title = re.sub(r'\\\w+({[^}]*})?', '', title.group(1))
+
+ iter_lines = iter(src_text.splitlines())
for l in iter_lines:
l = process_line(l)
if not l:
continue
if want_header:
add_header_line(replace_rst_macros('', l))
- m = search_title(l)
- if m:
- title = m.group(0)
if l.startswith("\\maketitle"):
break
@@ -155,10 +180,10 @@ def tex_postprocess(src, dest, want_header = False, process_line=noop):
break
elif "listcnt0" in l:
l = l.replace("listcnt0", counter_text)
- dest.write(l)
+ dest.write(l + '\n')
if not title:
- raise Exception("Bueee, no title")
+ raise Exception("Bueee, no title in %s" % src_path)
return title, header
def publish(dirname, lxml_path, release):
@@ -205,7 +230,6 @@ def fix_relative_hyperrefs(line):
return replace_docinternal_hyperrefs(r'\hyperref[\1]', line)
# Building pages
- have_epydoc_macros = False
for section, text_files in SITE_STRUCTURE:
for filename in text_files:
if filename.startswith('@'):
@@ -270,9 +294,7 @@ def fix_changelog(line):
for hln in header:
if hln.startswith(r"\documentclass"):
#hln = hln.replace('article', 'book')
- hln = DOCUMENT_CLASS
- elif hln.startswith("%% generator ") or hln.startswith("% generated "):
- master.write(EPYDOC_IMPORT)
+ hln = DOCUMENT_CLASS + EPYDOC_IMPORT
elif hln.startswith(r"\begin{document}"):
# pygments and epydoc support
master.write(PYGMENTS_IMPORT)
@@ -285,7 +307,7 @@ def fix_changelog(line):
elif hln.startswith("pdftitle"):
hln = replace_content(
r'{%s}' % book_title, hln)
- master.write(hln)
+ master.write(hln + '\n')
master.write("\\setcounter{page}{2}\n")
master.write("\\tableofcontents\n")

0 comments on commit 6d2a084

Please sign in to comment.