yaml.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #Copyright ReportLab Europe Ltd. 2000-2017
  2. #see license.txt for license details
  3. #history https://hg.reportlab.com/hg-public/reportlab/log/tip/src/reportlab/lib/yaml.py
  4. # parses "Yet Another Markup Language" into a list of tuples.
  5. # Each tuple says what the data is e.g.
  6. # ('Paragraph', 'Heading1', 'Why Reportlab Rules')
  7. # and the pattern depends on type.
  8. """
  9. .h1 Welcome to YAML!
  10. YAML is "Yet Another Markup Language" - a markup language
  11. which is easier to type in than XML, yet gives us a
  12. reasonable selection of formats.
  13. The general rule is that if a line begins with a '.',
  14. it requires special processing. Otherwise lines
  15. are concatenated to paragraphs, and blank lines
  16. separate paragraphs.
  17. If the line ".foo bar bletch" is encountered,
  18. it immediately ends and writes out any current
  19. paragraph.
  20. It then looks for a parser method called 'foo';
  21. if found, it is called with arguments (bar, bletch).
  22. If this is not found, it assumes that 'foo' is a
  23. paragraph style, and the text for the first line
  24. of the paragraph is 'bar bletch'. It would be
  25. up to the formatter to decide whether on not 'foo'
  26. was a valid paragraph.
  27. Special commands understood at present are:
  28. dot image filename
  29. - adds the image to the document
  30. dot beginPre Code
  31. - begins a Preformatted object in style 'Code'
  32. dot endPre
  33. - ends a preformatted object.
  34. """
  35. __version__='3.3.0'
  36. import sys
  37. #modes:
  38. PLAIN = 1
  39. PREFORMATTED = 2
  40. BULLETCHAR = '\267' # assumes font Symbol, but works on all platforms
  41. class BaseParser:
  42. """"Simplest possible parser with only the most basic options.
  43. This defines the line-handling abilities and basic mechanism.
  44. The class YAMLParser includes capabilities for a fairly rich
  45. story."""
  46. def __init__(self):
  47. self.reset()
  48. def reset(self):
  49. self._lineNo = 0
  50. self._style = 'Normal' # the default
  51. self._results = []
  52. self._buf = []
  53. self._mode = PLAIN
  54. def parseFile(self, filename):
  55. #returns list of objects
  56. data = open(filename, 'r').readlines()
  57. for line in data:
  58. #strip trailing newlines
  59. self.readLine(line[:-1])
  60. self.endPara()
  61. return self._results
  62. def parseText(self, textBlock):
  63. "Parses the a possible multi-line text block"
  64. lines = textBlock.split('\n')
  65. for line in lines:
  66. self.readLine(line)
  67. self.endPara()
  68. return self._results
  69. def readLine(self, line):
  70. #this is the inner loop
  71. self._lineNo = self._lineNo + 1
  72. stripped = line.lstrip()
  73. if len(stripped) == 0:
  74. if self._mode == PLAIN:
  75. self.endPara()
  76. else: #preformatted, append it
  77. self._buf.append(line)
  78. elif line[0]=='.':
  79. # we have a command of some kind
  80. self.endPara()
  81. words = stripped[1:].split()
  82. cmd, args = words[0], words[1:]
  83. #is it a parser method?
  84. if hasattr(self.__class__, cmd):
  85. #this was very bad; any type error in the method was hidden
  86. #we have to hack the traceback
  87. try:
  88. getattr(self,cmd)(*args)
  89. except TypeError as err:
  90. sys.stderr.write("Parser method: %s(*%s) %s at line %d\n" % (cmd, args, err, self._lineNo))
  91. raise
  92. else:
  93. # assume it is a paragraph style -
  94. # becomes the formatter's problem
  95. self.endPara() #end the last one
  96. words = stripped.split(' ', 1)
  97. assert len(words)==2, "Style %s but no data at line %d" % (words[0], self._lineNo)
  98. (styletag, data) = words
  99. self._style = styletag[1:]
  100. self._buf.append(data)
  101. else:
  102. #we have data, add to para
  103. self._buf.append(line)
  104. def endPara(self):
  105. #ends the current paragraph, or preformatted block
  106. text = ' '.join(self._buf)
  107. if text:
  108. if self._mode == PREFORMATTED:
  109. #item 3 is list of lines
  110. self._results.append(('PREFORMATTED', self._style,
  111. '\n'.join(self._buf)))
  112. else:
  113. self._results.append(('PARAGRAPH', self._style, text))
  114. self._buf = []
  115. self._style = 'Normal'
  116. def beginPre(self, stylename):
  117. self._mode = PREFORMATTED
  118. self._style = stylename
  119. def endPre(self):
  120. self.endPara()
  121. self._mode = PLAIN
  122. def image(self, filename):
  123. self.endPara()
  124. self._results.append(('IMAGE', filename))
  125. class Parser(BaseParser):
  126. """This adds a basic set of "story" components compatible with HTML & PDF.
  127. Images, spaces"""
  128. def vSpace(self, points):
  129. """Inserts a vertical spacer"""
  130. self._results.append(('VSpace', points))
  131. def pageBreak(self):
  132. """Inserts a frame break"""
  133. self._results.append(('PageBreak','blah')) # must be a tuple
  134. def custom(self, moduleName, funcName):
  135. """Goes and gets the Python object and adds it to the story"""
  136. self.endPara()
  137. self._results.append(('Custom',moduleName, funcName))
  138. def nextPageTemplate(self, templateName):
  139. self._results.append(('NextPageTemplate',templateName))
  140. def parseFile(filename):
  141. p = Parser()
  142. return p.parseFile(filename)
  143. def parseText(textBlock):
  144. p = Parser()
  145. return p.parseText(textBlock)
  146. if __name__=='__main__': #NORUNTESTS
  147. if len(sys.argv) != 2:
  148. results = parseText(__doc__)
  149. else:
  150. results = parseFile(sys.argv[1])
  151. import pprint
  152. pprint.pprint(results)