Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(751)

Side by Side Diff: third_party/Python-Markdown/markdown/extensions/headerid.py

Issue 1389543003: Revert of Check in a simple pure-python based Markdown previewer. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@add
Patch Set: Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 """
2 HeaderID Extension for Python-Markdown
3 ======================================
4
5 Auto-generate id attributes for HTML headers.
6
7 See <https://pythonhosted.org/Markdown/extensions/header_id.html>
8 for documentation.
9
10 Original code Copyright 2007-2011 [Waylan Limberg](http://achinghead.com/).
11
12 All changes Copyright 2011-2014 The Python Markdown Project
13
14 License: [BSD](http://www.opensource.org/licenses/bsd-license.php)
15
16 """
17
18 from __future__ import absolute_import
19 from __future__ import unicode_literals
20 from . import Extension
21 from ..treeprocessors import Treeprocessor
22 from ..util import parseBoolValue
23 from .toc import slugify, unique, stashedHTML2text
24 import warnings
25
26
27 class HeaderIdTreeprocessor(Treeprocessor):
28 """ Assign IDs to headers. """
29
30 IDs = set()
31
32 def run(self, doc):
33 start_level, force_id = self._get_meta()
34 slugify = self.config['slugify']
35 sep = self.config['separator']
36 for elem in doc:
37 if elem.tag in ['h1', 'h2', 'h3', 'h4', 'h5', 'h6']:
38 if force_id:
39 if "id" in elem.attrib:
40 id = elem.get('id')
41 else:
42 id = stashedHTML2text(''.join(elem.itertext()), self.md)
43 id = slugify(id, sep)
44 elem.set('id', unique(id, self.IDs))
45 if start_level:
46 level = int(elem.tag[-1]) + start_level
47 if level > 6:
48 level = 6
49 elem.tag = 'h%d' % level
50
51 def _get_meta(self):
52 """ Return meta data suported by this ext as a tuple """
53 level = int(self.config['level']) - 1
54 force = parseBoolValue(self.config['forceid'])
55 if hasattr(self.md, 'Meta'):
56 if 'header_level' in self.md.Meta:
57 level = int(self.md.Meta['header_level'][0]) - 1
58 if 'header_forceid' in self.md.Meta:
59 force = parseBoolValue(self.md.Meta['header_forceid'][0])
60 return level, force
61
62
63 class HeaderIdExtension(Extension):
64 def __init__(self, *args, **kwargs):
65 # set defaults
66 self.config = {
67 'level': ['1', 'Base level for headers.'],
68 'forceid': ['True', 'Force all headers to have an id.'],
69 'separator': ['-', 'Word separator.'],
70 'slugify': [slugify, 'Callable to generate anchors']
71 }
72
73 super(HeaderIdExtension, self).__init__(*args, **kwargs)
74
75 warnings.warn(
76 'The HeaderId Extension is pending deprecation. Use the TOC Extensio n instead.',
77 PendingDeprecationWarning
78 )
79
80 def extendMarkdown(self, md, md_globals):
81 md.registerExtension(self)
82 self.processor = HeaderIdTreeprocessor()
83 self.processor.md = md
84 self.processor.config = self.getConfigs()
85 if 'attr_list' in md.treeprocessors.keys():
86 # insert after attr_list treeprocessor
87 md.treeprocessors.add('headerid', self.processor, '>attr_list')
88 else:
89 # insert after 'prettify' treeprocessor.
90 md.treeprocessors.add('headerid', self.processor, '>prettify')
91
92 def reset(self):
93 self.processor.IDs = set()
94
95
96 def makeExtension(*args, **kwargs):
97 return HeaderIdExtension(*args, **kwargs)
OLDNEW
« no previous file with comments | « third_party/Python-Markdown/markdown/extensions/footnotes.py ('k') | third_party/Python-Markdown/markdown/extensions/meta.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698