File tree Expand file tree Collapse file tree 2 files changed +22
-20
lines changed
Expand file tree Collapse file tree 2 files changed +22
-20
lines changed Original file line number Diff line number Diff line change 3737import posixpath
3838import re
3939import shutil
40+ import string
4041
4142
4243_LINK_REGEX = r'!?\[(.*?)\]\((.*?)\)'
@@ -127,3 +128,23 @@ def backup(filename):
127128 else :
128129 # Everything's fine, we can safely get rid of the backup.
129130 os .remove (filename + '.backup' )
131+
132+
133+ def header_link (title ):
134+ """Return a github-style link target for a title.
135+
136+ >>> header_link('Hello there!')
137+ 'hello-there'
138+ """
139+ # This doesn't handle multiple titles with the same text in the
140+ # same file, but usually that's not a problem. GitHub makes
141+ # links like the-title, the-title-1, the-title-2 etc.
142+ result = ''
143+ for character in title :
144+ if character in string .whitespace :
145+ result += '-'
146+ elif character in string .punctuation :
147+ pass
148+ else :
149+ result += character .lower ()
150+ return result
Original file line number Diff line number Diff line change @@ -101,31 +101,12 @@ def __init__(self, pygments_style):
101101 self .pygments_style = pygments_style
102102 self .title = None # will be set by header()
103103
104- def _get_header_link (self , title ):
105- """Return a github-style link target for a title.
106-
107- >>> TutorialRenderer()._get_header_link('Hello there!')
108- 'hello-there'
109- """
110- # This doesn't handle multiple titles with the same text in the
111- # same file, but usually that's not a problem. GitHub makes
112- # links like the-title, the-title-1, the-title-2 etc.
113- result = ''
114- for character in title :
115- if character in string .whitespace :
116- result += '-'
117- elif character in string .punctuation :
118- pass
119- else :
120- result += character .lower ()
121- return result
122-
123104 def header (self , text , level , raw ):
124105 """Create a header that is also a link and a # link target."""
125106 # "# raw"
126107 if level == 1 :
127108 self .title = text
128- target = self . _get_header_link (raw )
109+ target = common . header_link (raw )
129110 content = super ().header (text , level , raw )
130111 return '<a name="{0}" href="#{0}">{1}</a>' .format (target , content )
131112
You can’t perform that action at this time.
0 commit comments