From 72d7d89c05d0b46a9dbe59c8046fb964de3ecf74 Mon Sep 17 00:00:00 2001 From: Leonid Grinberg Date: Sun, 29 Jan 2012 02:39:57 -0500 Subject: [PATCH] Make TeXCollection's start, end, and delimiters be compileable TeXObjects While this doesn't break backwards-compatibility (as usual, strings get wrapped in possibly unnecessary TeXObjects internally), this allows for potentially more interesting behavior in the future, where objects have different start/end/delimiters based on some programmable characteristic. --- pytex/base/collections.py | 41 +++++++++++++++++++++------------------ pytex/latex.py | 14 ++++++------- 2 files changed, 29 insertions(+), 26 deletions(-) diff --git a/pytex/base/collections.py b/pytex/base/collections.py index 9565a66..e9be235 100644 --- a/pytex/base/collections.py +++ b/pytex/base/collections.py @@ -6,9 +6,9 @@ class TeXCollection(TeXObject): enclosed in some starting and ending strings and may have some separator. """ - start = "" - end = "" - sep = " " + start = TeXObject("") + end = TeXObject("") + sep = TeXObject(" ") def __init__(self, objs=None, start=None, end=None, sep=None): if objs is None: objs = [] @@ -16,23 +16,26 @@ def __init__(self, objs=None, start=None, end=None, sep=None): objs = [objs] self.objs = objs if start is not None: - self.start = start + self.start = TeXObject(start) if end is not None: - self.end = end + self.end = TeXObject(end) if sep is not None: - self.sep = sep + self.sep = TeXObject(sep) def addObj(self, obj): self.objs.append(obj) def compile(self): - return self.start + self.sep.join(map(lambda o: TeXObject(o).compile(), - self.objs)) + self.end + start = self.start.compile() + sep = self.sep.compile() + objs = map(lambda o: TeXObject(o).compile(), self.objs) + end = self.end.compile() + return "".join([start, sep.join(objs), end]) class TeXSet(TeXCollection): """ A collection with no separator. This is useful for e.g a bunch of commands following one another """ - sep = "" + sep = TeXObject() class TeXGroup(TeXCollection): """ @@ -40,22 +43,22 @@ class TeXGroup(TeXCollection): (e.g. "{\bf foo bar baz} quux" will limit the effect of the "\bf" to "foo bar baz". """ - start = "{" - end = "}" + start = TeXObject("{", raw=True) + end = TeXObject("}", raw=True) class TeXInlineMath(TeXCollection): - start = "$" - end = "$" - sep = "" + start = TeXObject("$", raw=True) + end = TeXObject("$", raw=True) + sep = TeXObject() class TeXBlockMath(TeXCollection): - start = "$$" - end = "$$" - sep = "" + start = TeXObject("$$", raw=True) + end = TeXObject("$$", raw=True) + sep = TeXObject() class TeXRow(TeXCollection): """ A "row" that requires any sort of alignment or newline markers """ - end = "\\\\" - sep = " & " + end = TeXObject("\\\\", raw=True) + sep = TeXObject(" & ", raw=True) diff --git a/pytex/latex.py b/pytex/latex.py index faca337..3bb624d 100644 --- a/pytex/latex.py +++ b/pytex/latex.py @@ -1,23 +1,23 @@ -from base.objects import TeXCommand -from base.collections import TeXCollection, TeXRow +from base.objects import TeXObject, TeXCommand +from base.collections import TeXCollection, TeXRow, TeXSet class LaTeXEnvironment(TeXCollection): def __init__(self, env, objs=None): TeXCollection.__init__(self, objs) # We need to add the \n by hand here because the separator # isn't included for the start/end delimiters - self.start = TeXCommand("begin", env).compile() + "\n" - self.end = "\n" + TeXCommand("end", env).compile() - self.sep = "\n" + self.start = TeXSet([TeXCommand("begin", env), "\n"]) + self.end = TeXSet(["\n", TeXCommand("end", env)]) + self.sep = TeXObject("\n") class LaTeXTabular(LaTeXEnvironment): def __init__(self, env, colfmt, objs=None): LaTeXEnvironment.__init__(self, env, objs) - self.start = TeXCommand("begin", env, colfmt).compile() + "\n" + self.start = TeXSet([TeXCommand("begin", env, colfmt), "\n"]) def addObj(self, obj): if len(self.objs) > 0: self.objs[-1].end = TeXRow.end - obj.end = "" + obj.end = TeXObject() LaTeXEnvironment.addObj(self, obj) def simple_latex_document(body, packages=None, pagestyle="empty"):