Gets headlines from the markdown document and creates anchor tags. Keyword arguments: lines: a list of sublists where every sublist represents a line from a Markdown document. id_tag: if true, creates inserts a the tags (not req. by GitHub) back_l
(lines, id_tag=True, back_links=False, exclude_h=None)
| 107 | return [stripped_wspace, dashified, level] |
| 108 | |
| 109 | def tagAndCollect(lines, id_tag=True, back_links=False, exclude_h=None): |
| 110 | """ |
| 111 | Gets headlines from the markdown document and creates anchor tags. |
| 112 | |
| 113 | Keyword arguments: |
| 114 | lines: a list of sublists where every sublist |
| 115 | represents a line from a Markdown document. |
| 116 | id_tag: if true, creates inserts a the <a id> tags (not req. by GitHub) |
| 117 | back_links: if true, adds "back to top" links below each headline |
| 118 | exclude_h: header levels to exclude. E.g., [2, 3] |
| 119 | excludes level 2 and 3 headings. |
| 120 | |
| 121 | Returns a tuple of 2 lists: |
| 122 | 1st list: |
| 123 | A modified version of the input list where |
| 124 | <a id="some-header"></a> anchor tags where inserted |
| 125 | above the header lines (if github is False). |
| 126 | |
| 127 | 2nd list: |
| 128 | A list of 3-value sublists, where the first value |
| 129 | represents the heading, the second value the string |
| 130 | that was inserted assigned to the IDs in the anchor tags, |
| 131 | and the third value is an integer that represents the headline level. |
| 132 | E.g., |
| 133 | [['some header lvl3', 'some-header-lvl3', 3], ...] |
| 134 | |
| 135 | """ |
| 136 | out_contents = [] |
| 137 | headlines = [] |
| 138 | for l in lines: |
| 139 | saw_headline = False |
| 140 | |
| 141 | orig_len = len(l) |
| 142 | l_stripped = l.lstrip() |
| 143 | |
| 144 | if l_stripped.startswith(('# ', '## ', '### ', '#### ', '##### ', '###### ')): |
| 145 | |
| 146 | # comply with new markdown standards |
| 147 | |
| 148 | # not a headline if '#' not followed by whitespace '##no-header': |
| 149 | if not l.lstrip('#').startswith(' '): |
| 150 | continue |
| 151 | # not a headline if more than 6 '#': |
| 152 | if len(l) - len(l.lstrip('#')) > 6: |
| 153 | continue |
| 154 | # headers can be indented by at most 3 spaces: |
| 155 | if orig_len - len(l_stripped) > 3: |
| 156 | continue |
| 157 | |
| 158 | # ignore empty headers |
| 159 | if not set(l) - {'#', ' '}: |
| 160 | continue |
| 161 | |
| 162 | saw_headline = True |
| 163 | dashified = dashifyHeadline(l) |
| 164 | |
| 165 | if not exclude_h or not dashified[-1] in exclude_h: |
| 166 | if id_tag: |
no test coverage detected