Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Run makeindex when *.idx files were created during PDF build. #32

Merged
merged 1 commit into from

2 participants

@jone
Owner

This allows to create indexes easily just by using the latex commands.
The builder takes care that the index is built and the PDF rebuilt properly.

For creating an index in a LaTeX PDF the \index and \printindex commands are used.
For building the index so that it can include the page numbers of occurrences, one needs to build the PDF (pdflatex), then build the index (makeindex), then again run pdflatex for including the built index in the PDF.

/ @maethu

@jone jone Run makeindex when *.idx files were created during PDF build.
This allows to create indexes easily just by using the latex commands.
The builder takes care that the index is built and the PDF rebuilt properly.
14b1cc0
@maethu maethu merged commit ea795d5 into master
@maethu maethu deleted the jone-run-makeindex branch
@jone jone referenced this pull request in 4teamwork/ftw.book
Merged

Keywords and keyword index #52

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 3, 2014
  1. @jone

    Run makeindex when *.idx files were created during PDF build.

    jone authored
    This allows to create indexes easily just by using the latex commands.
    The builder takes care that the index is built and the PDF rebuilt properly.
This page is out of date. Refresh to see the latest.
View
5 docs/HISTORY.txt
@@ -4,7 +4,10 @@ Changelog
1.2.11 (unreleased)
-------------------
-- Nothing changed yet.
+- Run makeindex when *.idx files were created during PDF build.
+ This allows to create indexes easily just by using the latex commands.
+ The builder takes care that the index is built and the PDF rebuilt properly.
+ [jone]
1.2.10 (2014-02-05)
View
13 ftw/pdfgenerator/builder.py
@@ -89,16 +89,27 @@ def _build_pdf(self, latex):
latex_file.write(latex)
latex_file.close()
+ cmd = 'pdflatex --interaction=nonstopmode %s' % latex_path
stdout = ''
while self._rerun_required(stdout):
- cmd = 'pdflatex --interaction=nonstopmode %s' % latex_path
_exitcode, stdout, _stderr = self._execute(cmd)
+ if self._makeindex():
+ self._execute(cmd)
+
if not os.path.exists(pdf_path):
raise PDFBuildFailed('PDF missing.')
return pdf_path
+ def _makeindex(self):
+ idx_path = os.path.join(self.build_directory, 'export.idx')
+ if not os.path.exists(idx_path):
+ return False
+
+ self._execute('makeindex export')
+ return True
+
def _rerun_required(self, stdout):
if self._rerun_limit == 0:
raise PDFBuildFailed('Maximum pdf build limit reached.')
View
58 ftw/pdfgenerator/tests/test_builder.py
@@ -261,6 +261,64 @@ def exec_mock(cmd):
self.assertEqual(str(cm.exception),
'Maximum pdf build limit reached.')
+ def test_build_pdf_executes_makeindex(self):
+ builder = self.mocker.patch(getUtility(IBuilderFactory)())
+ aux_path = os.path.join(self.builddir, 'export.aux')
+ pdf_path = os.path.join(self.builddir, 'export.pdf')
+
+ def pdflatex_call_mock(cmd):
+ with open(aux_path, 'w+') as aux:
+ aux.write('first run')
+ with open(pdf_path, 'w+') as pdf:
+ pdf.write('the pdf')
+ return (0, 'the log', '')
+
+ # Runs 2 times by default, does not rerun because _makeindex
+ # returns False (non rerun required)
+ self.expect(builder._execute(ANY)).call(pdflatex_call_mock).count(2)
+ self.expect(builder._makeindex()).result(False)
+
+ self.replay()
+
+ builder._build_pdf(u'LaTeX')
+
+ def test_build_pdf_reruns_when_makeindex_requires_rerun(self):
+ builder = self.mocker.patch(getUtility(IBuilderFactory)())
+ aux_path = os.path.join(self.builddir, 'export.aux')
+ pdf_path = os.path.join(self.builddir, 'export.pdf')
+
+ def pdflatex_call_mock(cmd):
+ with open(aux_path, 'w+') as aux:
+ aux.write('first run')
+ with open(pdf_path, 'w+') as pdf:
+ pdf.write('the pdf')
+ return (0, 'the log', '')
+
+ # Runs 3 times: 2 by default + once because _makeindex requires
+ # an additional run by returning True
+ self.expect(builder._execute(ANY)).call(pdflatex_call_mock).count(3)
+ self.expect(builder._makeindex()).result(True)
+
+ self.replay()
+
+ builder._build_pdf(u'LaTeX')
+
+ def test_makeindex_does_nothing_and_returns_False_without_idx_file(self):
+ builder = getUtility(IBuilderFactory)()
+ self.assertEquals(False, builder._makeindex())
+
+ def test_makeindex_calls_executable_and_returns_True_with_idx_file(self):
+ builder = self.mocker.patch(getUtility(IBuilderFactory)())
+ idx_path = os.path.join(self.builddir, 'export.idx')
+ with open(idx_path, 'w+') as idx:
+ idx.write('\indexentry{Test}{2}')
+
+ (self.expect(builder._execute('makeindex export'))
+ .result((0, 'stdout', 'stderr')))
+
+ self.replay()
+ self.assertEquals(True, builder._makeindex())
+
def test_cleanup(self):
builder = getUtility(IBuilderFactory)()
self.assertTrue(os.path.exists(builder.build_directory))
Something went wrong with that request. Please try again.