Changeset 42 for bitstructures/trunk/substructure/codeblocks.py
- Timestamp:
- 01/06/08 14:41:54 (11 months ago)
- Files:
-
- 1 modified
-
bitstructures/trunk/substructure/codeblocks.py (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
bitstructures/trunk/substructure/codeblocks.py
r25 r42 18 18 return self.text 19 19 20 def is_codeblock_header_field(line): 21 if line.is_codeblock_line() and get_field_name(line): 22 return True 23 else: 24 return False 25 26 def get_field_name(header_line): 27 name_value = header_line.get_text_without_indentation() 28 index = name_value.find(':') 29 if index != -1: 30 return name_value[0:index].strip() 31 else: 32 return None 33 34 def get_field_value(header_line): 35 name_value = header_line.get_text_without_indentation() 36 index = name_value.find(':') 37 if index != -1: 38 return name_value[index+1:].strip() 39 else: 40 return None 41 20 42 class MarkdownLineTokeniser: 21 43 def __init__(self, text): 22 44 self._lines = iter(text.splitlines(True)) 23 self. _current_line = None45 self.current_line = None 24 46 self._at_end = False 25 26 def get_current_line(self):27 return self._current_line28 47 29 48 def is_at_end(self): … … 32 51 def read_next(self): 33 52 try: 34 self. _current_line = MarkdownLine(self._lines.next())53 self.current_line = MarkdownLine(self._lines.next()) 35 54 except StopIteration: 36 55 self._at_end = True 37 self._current_line = None 56 self.current_line = None 57 58 class Section: 59 def __init__(self): 60 self.lines = list() 61 62 def get_text(self): 63 return ''.join(line.text for line in self.lines) 64 65 def append(self, line): 66 self.lines.append(line) 67 68 class Codeblock(Section): 69 def __init__(self): 70 Section.__init__(self) 71 self.content_type = None 72 73 def is_codeblock(self): 74 return True 75 76 def get_code(self): 77 return ''.join(line.get_text_without_indentation() for line in self.lines) 78 79 class NonCodeblock(Section): 80 def is_codeblock(self): 81 return False 38 82 39 83 class MarkdownCodeblocksParser: … … 45 89 lines.read_next() 46 90 while not lines.is_at_end(): 47 if lines. get_current_line().is_indented():48 codeblock = self._get_codeblock _text(lines)91 if lines.current_line.is_indented(): 92 codeblock = self._get_codeblock(lines) 49 93 count += 1 50 94 if count == n: … … 53 97 return None 54 98 55 def _get_codeblock_text(self, lines): 56 text = str() 57 while (not lines.is_at_end()) and lines.get_current_line().is_codeblock_line(): 58 text = text + lines.get_current_line().get_text_without_indentation() 99 def parse(self, text): 100 sections = list() 101 lines = MarkdownLineTokeniser(text) 102 lines.read_next() 103 while not lines.is_at_end(): 104 if lines.current_line.is_indented(): 105 sections.append(self._get_codeblock(lines)) 106 else: 107 sections.append(self._get_non_codeblock(lines)) 108 return sections 109 110 def _get_codeblock(self, lines): 111 codeblock = Codeblock() 112 if is_codeblock_header_field(lines.current_line): 113 if get_field_name(lines.current_line) == 'Content-Type': 114 codeblock.content_type = get_field_value(lines.current_line) 59 115 lines.read_next() 60 return text 116 while (not lines.is_at_end()) and lines.current_line.is_codeblock_line(): 117 codeblock.append(lines.current_line) 118 lines.read_next() 119 return codeblock 120 121 def _get_non_codeblock(self, lines): 122 section = NonCodeblock() 123 while (not lines.is_at_end()) and (not lines.current_line.is_indented()): 124 section.append(lines.current_line) 125 lines.read_next() 126 return section
