Show
Ignore:
Timestamp:
01/06/08 14:41:54 (11 months ago)
Author:
simon
Message:

implemented syntax highlighting for codeblocks

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • bitstructures/trunk/substructure/codeblocks.py

    r25 r42  
    1818            return self.text 
    1919 
     20def 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 
     26def 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 
     34def 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 
    2042class MarkdownLineTokeniser: 
    2143    def __init__(self, text): 
    2244        self._lines = iter(text.splitlines(True)) 
    23         self._current_line = None 
     45        self.current_line = None 
    2446        self._at_end = False 
    25  
    26     def get_current_line(self): 
    27         return self._current_line 
    2847 
    2948    def is_at_end(self): 
     
    3251    def read_next(self): 
    3352        try: 
    34             self._current_line = MarkdownLine(self._lines.next()) 
     53            self.current_line = MarkdownLine(self._lines.next()) 
    3554        except StopIteration: 
    3655            self._at_end = True 
    37             self._current_line = None 
     56            self.current_line = None 
     57 
     58class 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 
     68class 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 
     79class NonCodeblock(Section): 
     80    def is_codeblock(self): 
     81        return False 
    3882 
    3983class MarkdownCodeblocksParser: 
     
    4589        lines.read_next() 
    4690        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) 
    4993                count += 1 
    5094                if count == n: 
     
    5397        return None 
    5498 
    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) 
    59115            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