File tree 2 files changed +22
-20
lines changed
2 files changed +22
-20
lines changed Original file line number Diff line number Diff line change 37
37
import posixpath
38
38
import re
39
39
import shutil
40
+ import string
40
41
41
42
42
43
_LINK_REGEX = r'!?\[(.*?)\]\((.*?)\)'
@@ -127,3 +128,23 @@ def backup(filename):
127
128
else :
128
129
# Everything's fine, we can safely get rid of the backup.
129
130
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):
101
101
self .pygments_style = pygments_style
102
102
self .title = None # will be set by header()
103
103
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
-
123
104
def header (self , text , level , raw ):
124
105
"""Create a header that is also a link and a # link target."""
125
106
# "# raw"
126
107
if level == 1 :
127
108
self .title = text
128
- target = self . _get_header_link (raw )
109
+ target = common . header_link (raw )
129
110
content = super ().header (text , level , raw )
130
111
return '<a name="{0}" href="#{0}">{1}</a>' .format (target , content )
131
112
You can’t perform that action at this time.
0 commit comments