Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

text_writer.py 3.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # Copyright (c) 2015-2016 Claudiu Popa <pcmanticore@gmail.com>
  2. # Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  3. # For details: https://github.com/PyCQA/pylint/blob/master/COPYING
  4. """Text formatting drivers for ureports"""
  5. from __future__ import print_function
  6. from pylint.reporters.ureports import BaseWriter
  7. TITLE_UNDERLINES = [u'', u'=', u'-', u'`', u'.', u'~', u'^']
  8. BULLETS = [u'*', u'-']
  9. class TextWriter(BaseWriter):
  10. """format layouts as text
  11. (ReStructured inspiration but not totally handled yet)
  12. """
  13. def begin_format(self):
  14. super(TextWriter, self).begin_format()
  15. self.list_level = 0
  16. def visit_section(self, layout):
  17. """display a section as text
  18. """
  19. self.section += 1
  20. self.writeln()
  21. self.format_children(layout)
  22. self.section -= 1
  23. self.writeln()
  24. def visit_evaluationsection(self, layout):
  25. """Display an evaluation section as a text."""
  26. self.section += 1
  27. self.format_children(layout)
  28. self.section -= 1
  29. self.writeln()
  30. def visit_title(self, layout):
  31. title = u''.join(list(self.compute_content(layout)))
  32. self.writeln(title)
  33. try:
  34. self.writeln(TITLE_UNDERLINES[self.section] * len(title))
  35. except IndexError:
  36. print("FIXME TITLE TOO DEEP. TURNING TITLE INTO TEXT")
  37. def visit_paragraph(self, layout):
  38. """enter a paragraph"""
  39. self.format_children(layout)
  40. self.writeln()
  41. def visit_table(self, layout):
  42. """display a table as text"""
  43. table_content = self.get_table_content(layout)
  44. # get columns width
  45. cols_width = [0]*len(table_content[0])
  46. for row in table_content:
  47. for index, col in enumerate(row):
  48. cols_width[index] = max(cols_width[index], len(col))
  49. self.default_table(layout, table_content, cols_width)
  50. self.writeln()
  51. def default_table(self, layout, table_content, cols_width):
  52. """format a table"""
  53. cols_width = [size+1 for size in cols_width]
  54. format_strings = u' '.join([u'%%-%ss'] * len(cols_width))
  55. format_strings = format_strings % tuple(cols_width)
  56. format_strings = format_strings.split(u' ')
  57. table_linesep = u'\n+' + u'+'.join([u'-'*w for w in cols_width]) + u'+\n'
  58. headsep = u'\n+' + u'+'.join([u'='*w for w in cols_width]) + u'+\n'
  59. # FIXME: layout.cheaders
  60. self.write(table_linesep)
  61. for index, line in enumerate(table_content):
  62. self.write(u'|')
  63. for line_index, at_index in enumerate(line):
  64. self.write(format_strings[line_index] % at_index)
  65. self.write(u'|')
  66. if index == 0 and layout.rheaders:
  67. self.write(headsep)
  68. else:
  69. self.write(table_linesep)
  70. def visit_verbatimtext(self, layout):
  71. """display a verbatim layout as text (so difficult ;)
  72. """
  73. self.writeln(u'::\n')
  74. for line in layout.data.splitlines():
  75. self.writeln(u' ' + line)
  76. self.writeln()
  77. def visit_text(self, layout):
  78. """add some text"""
  79. self.write(u'%s' % layout.data)