Skip to content

Commit

Permalink
Adding support for the 'overpic' environment (see Issue #2)
Browse files Browse the repository at this point in the history
  • Loading branch information
fchauvel committed Aug 27, 2015
1 parent 0bf7d61 commit 13f0744
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
24 changes: 20 additions & 4 deletions flap/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,12 @@ def inputMerger(file, proxy):
return InputFlattener(CommentsRemover(FileWrapper(file)), proxy)

@staticmethod
def flapPipeline(proxy):
return IncludeSVGFixer(IncludeGraphicsAdjuster(IncludeFlattener(Processor.inputMerger(proxy.file(), proxy), proxy), proxy), proxy)
def flap_pipeline(proxy):
processors = [IncludeFlattener, IncludeGraphicsAdjuster, IncludeSVGFixer, OverpicAdjuster]
pipeline = Processor.inputMerger(proxy.file(), proxy)
for eachProcessor in processors:
pipeline = eachProcessor(pipeline, proxy)
return pipeline


class FileWrapper(Processor):
Expand All @@ -133,6 +137,7 @@ def fragments(self):
#yield from [ Fragment(self._file) ] Replaced for compatibility with Py3.2
yield Fragment(self._file)


class ProcessorDecorator(Processor):
"""
Abstract Decorator for processors
Expand Down Expand Up @@ -210,7 +215,6 @@ def preparePattern(self):
pass



class InputFlattener(RegexReplacer):
"""
Detects fragments that contains an input directive (such as '\input{foo}).
Expand Down Expand Up @@ -291,6 +295,18 @@ def notify(self, fragment, graphic):
return self.flap.onIncludeGraphics(fragment, graphic)


class OverpicAdjuster(IncludeGraphicsAdjuster):
"""
Adjust 'overpic' environment. Only the opening clause is adjusted.
"""

def __init__(self, delegate, proxy):
super().__init__(delegate, proxy)

def preparePattern(self):
pattern = r"\\begin{overpic}\s*(?:\[(?:[^\]]+)\])*\{([^\}]+)\}"
return re.compile(pattern)


class IncludeSVGFixer(IncludeGraphicsAdjuster):
"""
Expand Down Expand Up @@ -336,7 +352,7 @@ def file(self):
return self._root

def mergeLaTeXSource(self):
pipeline = Processor.flapPipeline(self)
pipeline = Processor.flap_pipeline(self)
fragments = pipeline.fragments()
merge = ''.join([ f.text() for f in fragments ])
self._fileSystem.createFile(self._output / "merged.tex", merge)
Expand Down
23 changes: 23 additions & 0 deletions tests/unit/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,29 @@ def testMultilinesPath(self):

self.verifyFile(ROOT / "result" / "merged.tex", expected)

def test_overpic_environment_are_adjusted(self):
text = r"""
\begin{overpic}[scale=0.25,unit=1mm,grid,tics=10]{%
img/picture}
blablabla
\end{overpic}
"""

self.fileSystem.createFile(ROOT / "project" / "main.tex", text)
self.fileSystem.createFile(ROOT / "project" / "img" / "picture.pdf", "xyz")

self.flap.flatten(ROOT / "project" / "main.tex", ROOT / "result")

expected = r"""
\begin{overpic}[scale=0.25,unit=1mm,grid,tics=10]{picture}
blablabla
\end{overpic}
"""

self.verifyFile(ROOT / "result" / "merged.tex", expected)
self.verifyFile(ROOT / "result" / "picture.pdf", "xyz")


def testLinksToGraphicsAreRecursivelyAdjusted(self):
self.fileSystem.createFile(ROOT / "project" / "main.tex", r"AA \input{foo} AA")
self.fileSystem.createFile(ROOT / "project" / "foo.tex", r"BB \includegraphics[width=3cm]{img/foo} BB")
Expand Down

0 comments on commit 13f0744

Please sign in to comment.