Skip to content

Commit

Permalink
Merge 10a22a0 into 18fb67b
Browse files Browse the repository at this point in the history
  • Loading branch information
mgedmin committed Dec 5, 2014
2 parents 18fb67b + 10a22a0 commit 09b1501
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 7 deletions.
27 changes: 20 additions & 7 deletions mgp2pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ def addImage(self, filename, zoom=100, raised_by=0):
"""
line = self.currentOrNewLine()
line.add(Image(filename, zoom, raised_by))
# XXX: self.closeCurrentLine()?
self.closeCurrentLine()

def addMark(self):
"""Add a mark to the current line and return it.
Expand Down Expand Up @@ -798,6 +798,7 @@ def _handleDirective_page(self, parts):
self._lastlineno = 0
self._use_defaults = True
self._continuing = False
self._applying_defaults = False
self._directives_used_in_this_line = set()
self.mark = None

Expand Down Expand Up @@ -931,7 +932,12 @@ def _handleDirective_newimage(self, parts):
else:
raise MgpSyntaxError("newimage %s not handled yet" % k)
filename = os.path.join(self.basedir, args[-1])
if not self._continuing and not self._applying_defaults:
self._lastlineno += 1
self._applyDefaults()
self.slides[-1].addImage(filename, zoom, raised_by)
if self._applying_defaults:
self.slides[-1].reopenCurrentLine()

def _handleDirective_mark(self, parts):
"""Handle %mark.
Expand Down Expand Up @@ -970,7 +976,7 @@ def _handleDirective_again(self, parts):
"""
if not self.mark:
raise MgpSyntaxError("%again without %mark")
self._handleText('')
##self._handleText('')
self.slides[-1].addAgain(self.mark)

def _handleUnknownDirective(self, parts):
Expand Down Expand Up @@ -1018,17 +1024,24 @@ def _parseArgs(self, parts, argspec):
assert False, 'unknown argspec %r' % arg
return tuple(results)

def _applyDefaults(self):
"""Apply the default directives for this line"""
assert not self._applying_defaults
if self._use_defaults:
self._applying_defaults = True
for part in self.defaultDirectives.get(self._lastlineno, []):
word = self._splitArgs(part)[0]
if word not in self._directives_used_in_this_line:
self._handleDirective(part)
self._applying_defaults = False

def _handleText(self, line):
"""Handle a line of text that is not a comment or a directive."""
if self.inPreamble():
raise MgpSyntaxError('No text allowed in the preamble')
if not self._continuing:
self._lastlineno += 1
if self._use_defaults:
for part in self.defaultDirectives.get(self._lastlineno, []):
word = self._splitArgs(part)[0]
if word not in self._directives_used_in_this_line:
self._handleDirective(part)
self._applyDefaults()
line = line.rstrip('\n').replace(r'\#', '#').replace(r'\\', '\\')
self.slides[-1].addText(line)
self._continuing = False
Expand Down
10 changes: 10 additions & 0 deletions samples/synthetic/images.mgp
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
%default 1 fore "black", back "white", center
%page

When do images cause line breaks?

%newimage "povlogo.png"
%newimage "povlogo.png"
%newimage "povlogo.png"
text

Always
%page

So, -raise doesn't appear to do anything

%newimage "povlogo.png"
Expand Down
44 changes: 44 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,50 @@ def test_default(self):
self.assertRaises(mgp2pdf.MgpSyntaxError, p._handleDirectives,
'%default 1 left')

def test_application_of_defaults(self):
p = mgp2pdf.Presentation()
p.load(StringIO('\n'.join([
'%default 1 left',
'%default 2 center',
'%default 3 right',
'%page',
'a',
'b',
'%cont',
'b',
'c',
])))
self.assertEqual(str(p),
"--- Slide 1 ---\n"
"a\n"
"bb\n"
"c\n")
self.assertEqual([line.alignment for line in p.slides[0].lines],
[mgp2pdf.Left, mgp2pdf.Center, mgp2pdf.Right])

@mock.patch('mgp2pdf.ImageReader')
def test_application_of_defaults_when_images_are_involved(self, mock_ImageReader):
mock_ImageReader().getSize.return_value = 100, 50
p = mgp2pdf.Presentation()
p.load(StringIO('\n'.join([
'%default 1 left',
'%default 2 center',
'%default 3 right',
'%page',
'a',
'%newimage "b.png"',
'%cont',
'b',
'c',
])))
self.assertEqual(str(p),
"--- Slide 1 ---\n"
"a\n"
"[b.png]b\n"
"c\n")
self.assertEqual([line.alignment for line in p.slides[0].lines],
[mgp2pdf.Left, mgp2pdf.Center, mgp2pdf.Right])

def test_tab(self):
p = mgp2pdf.Presentation()
p._handleDirectives('%tab 1')
Expand Down

0 comments on commit 09b1501

Please sign in to comment.