formatter.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. ## @package formatter
  2. # Module caffe2.python.docs.formatter
  3. from caffe2.python.docs.parser import Parser
  4. class Formatter(object):
  5. def __init__(self):
  6. self.content = ""
  7. def clone(self):
  8. return self.__class__()
  9. def dump(self):
  10. return self.content
  11. def parseAndAdd(self, text):
  12. text = Parser(text, self).parse()
  13. self.addRaw(text)
  14. def addRaw(self, text):
  15. raise Exception('Not yet implemented.')
  16. def addLine(self, text):
  17. raise Exception('Not yet implemented.')
  18. def addLinebreak(self):
  19. raise Exception('Not yet implemented.')
  20. def addHeader(self, text):
  21. raise Exception('Not yet implemented.')
  22. def addEmphasis(self, text):
  23. raise Exception('Not yet implemented.')
  24. def addList(self, textList):
  25. raise Exception('Not yet implemented.')
  26. def addLink(self, text, url):
  27. raise Exception('Not yet implemented.')
  28. def addCode(self, text):
  29. raise Exception('Not yet implemented.')
  30. def addCodeLink(self, text):
  31. raise Exception('Not yet implemented.')
  32. def addTable(self, table):
  33. raise Exception('Not yet implemented.')
  34. def addBreak(self):
  35. raise Exception('Not yet implemented.')
  36. class Markdown(Formatter):
  37. def addRaw(self, text):
  38. self.content += "{text}".format(text=text)
  39. def addLine(self, text, new_line=False):
  40. self.content += "{line}{text}\n".format(line=('\n' if new_line else ''),
  41. text=text)
  42. def addLinebreak(self):
  43. self.content += "\n"
  44. def addHeader(self, text, h=1):
  45. self.addLine("{header} {text}".format(header=h * '#', text=text), True)
  46. def addEmphasis(self, text, s=1):
  47. self.addRaw("{stars}{text}{stars}".format(stars=s * '*', text=text))
  48. def addList(self, textList):
  49. for text in textList:
  50. self.addLine("- {text}".format(text=text), True)
  51. self.addLinebreak()
  52. def addLink(self, text, url):
  53. self.addRaw("[{text}]({url})".format(text=text, url=url))
  54. def addCodeLink(self, path, options=None):
  55. self.addRaw("({path})".format(path=path))
  56. def addCode(self, text, inline=False):
  57. if (inline):
  58. self.content += "`{text}`".format(text=text)
  59. else:
  60. self.addRaw("\n\n```\n{text}```\n\n".format(text=text))
  61. def addTable(self, table, noTitle=False):
  62. self.addLinebreak()
  63. assert(len(table) > 1)
  64. if noTitle:
  65. table.insert(0, [' ' for i in range(len(table[0]))])
  66. self.addLine(' | '.join(table[0]))
  67. self.addLine(' | '.join(['----' for i in range(len(table[0]))]))
  68. for row in table[1:]:
  69. self.addLine(' | '.join(row))
  70. self.addLinebreak()
  71. def addBreak(self):
  72. self.addLine('\n---\n', True)