Skip to content

Commit

Permalink
Make TeXCollection's start, end, and delimiters be compileable TeXObj…
Browse files Browse the repository at this point in the history
…ects

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.
  • Loading branch information
leonidg committed Jan 29, 2012
1 parent bd3257e commit 72d7d89
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 26 deletions.
41 changes: 22 additions & 19 deletions pytex/base/collections.py
Expand Up @@ -6,56 +6,59 @@ 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 = []
elif type(objs) is not list:
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):
"""
A semantically grouped collection. Often used for scope
(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)
14 changes: 7 additions & 7 deletions 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"):
Expand Down

0 comments on commit 72d7d89

Please sign in to comment.