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