Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix section number plugin to handle html title with attribut #1385

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ Markdown
nose
nose-exclude
nose-summary-report
beautifulsoup4
83 changes: 24 additions & 59 deletions section_number/section_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,76 +5,41 @@
Adds section numbers to section titles of the article
"""

import pelican.contents
from pelican import signals
from bs4 import BeautifulSoup


def _extract_level(text, idx):
end = text.find(">", idx)
def _insert_title_number(text: str, level_max: int) -> str:
levels_count = dict()
soup = BeautifulSoup(text, 'html.parser')
previous_level = 0

if end == -1:
return (idx, -1)
for head_tag in soup.find_all([f'h{i}' for i in range(1, level_max)],
recursive=True):
level = int(head_tag.name.replace('h', ''))

try:
level = int(text[idx: end])
return (end, level)
if level < previous_level:
for k in levels_count.keys():
if k > level:
levels_count[k] = 0

except:
return (idx, -1)
levels_count.setdefault(level, 0)
levels_count[level] += 1

top_head_string = ''
for top_level, top_level_count in levels_count.items():
if top_level < level and top_level_count != 0:
top_head_string = f'{top_head_string}{top_level_count}.'

def _level_str(level_nums, level_max):
ret = u''
head_tag.string = f'{top_head_string}{levels_count[level]}' \
f' {head_tag.string}'
previous_level = level

if len(level_nums) > level_max:
return ret
return soup.decode(formatter='html')

for n in level_nums:
ret += str(n) + '.'

return ret[:-1]


def _insert_title_number(text, level_max):
idx = 0
levels = []
level_nums = []

while True:
idx = text.find("<h", idx)
if idx == -1:
break

(idx, level) = _extract_level(text, idx + 2)

if level == -1:
continue

if not levels:
levels += [level]
level_nums += [1]

elif level == levels[-1]:
level_nums[-1] += 1

elif level < levels[-1]:
while level < levels[-1]:
levels.pop()
level_nums.pop()
level_nums[-1] += 1

else:
while level > levels[-1]:
levels += [levels[-1] + 1]
level_nums += [1]

text = text[:idx + 1] + \
_level_str(level_nums, level_max) + '. ' + text[idx + 1:]

# print text.encode('gb2312')
return text


def process_content(content):
def process_content(content: pelican.contents.Article):
if content._content is None:
return

Expand Down