OLD | NEW |
(Empty) | |
| 1 # Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 """Implements Gitiles' notification, aside and promotion blocks. |
| 6 |
| 7 This extention makes the Markdown parser recognize the Gitiles' extended |
| 8 blocks notation. The syntax is explained at: |
| 9 |
| 10 https://gerrit.googlesource.com/gitiles/+/master/Documentation/markdown.md#Notif
ication_aside_promotion-blocks |
| 11 """ |
| 12 |
| 13 from markdown.blockprocessors import BlockProcessor |
| 14 from markdown.extensions import Extension |
| 15 from markdown.util import etree |
| 16 import re |
| 17 |
| 18 |
| 19 class _GitilesExtBlockProcessor(BlockProcessor): |
| 20 """Process Gitiles' notification, aside and promotion blocks.""" |
| 21 |
| 22 RE_START = re.compile(r'^\*\*\* (note|aside|promo) *\n') |
| 23 RE_END = re.compile(r'\n\*\*\* *\n?$') |
| 24 |
| 25 def __init__(self, *args, **kwargs): |
| 26 self._last_parent = None |
| 27 BlockProcessor.__init__(self, *args, **kwargs) |
| 28 |
| 29 def test(self, parent, block): |
| 30 return self.RE_START.search(block) or self.RE_END.search(block) |
| 31 |
| 32 def run(self, parent, blocks): |
| 33 raw_block = blocks.pop(0) |
| 34 match_start = self.RE_START.search(raw_block) |
| 35 if match_start: |
| 36 # Opening a new block. |
| 37 rest = raw_block[match_start.end():] |
| 38 |
| 39 if self._last_parent: |
| 40 # Inconsistent state (nested starting markers). Ignore the marker |
| 41 # and keep going. |
| 42 blocks.insert(0, rest) |
| 43 return |
| 44 |
| 45 div = etree.SubElement(parent, 'div') |
| 46 # Setting the class name is sufficient, because doc.css already has |
| 47 # styles for these classes. |
| 48 div.set('class', match_start.group(1)) |
| 49 self._last_parent = parent |
| 50 blocks.insert(0, rest) |
| 51 self.parser.parseBlocks(div, blocks) |
| 52 return |
| 53 |
| 54 match_end = self.RE_END.search(raw_block) |
| 55 if match_end: |
| 56 # Ending an existing block. |
| 57 |
| 58 # Process the text preceding the ending marker in the current context |
| 59 # (i.e. within the div block). |
| 60 rest = raw_block[:match_end.start()] |
| 61 self.parser.parseBlocks(parent, [rest]) |
| 62 |
| 63 if not self._last_parent: |
| 64 # Inconsistent state (the ending marker is found but there is no |
| 65 # matching starting marker). |
| 66 # Let's continue as if we did not see the ending marker. |
| 67 return |
| 68 |
| 69 last_parent = self._last_parent |
| 70 self._last_parent = None |
| 71 self.parser.parseBlocks(last_parent, blocks) |
| 72 return |
| 73 |
| 74 |
| 75 class _GitilesExtBlockExtension(Extension): |
| 76 """Add Gitiles' extended blocks to Markdown.""" |
| 77 def extendMarkdown(self, md, md_globals): |
| 78 md.parser.blockprocessors.add('gitilesextblocks', |
| 79 _GitilesExtBlockProcessor(md.parser), |
| 80 '_begin') |
| 81 |
| 82 |
| 83 def makeExtension(*args, **kwargs): |
| 84 return _GitilesExtBlockExtension(*args, **kwargs) |
OLD | NEW |