Explorar el Código

Merge pull request #620 from potatola/master

New plugin: section numbering
Justin Mayer hace 9 años
padre
commit
f8fb3937d6
Se han modificado 4 ficheros con 131 adiciones y 0 borrados
  1. 2 0
      Readme.rst
  2. 39 0
      section_number/Readme.md
  3. 1 0
      section_number/__init__.py
  4. 89 0
      section_number/section_number.py

+ 2 - 0
Readme.rst

@@ -180,6 +180,8 @@ Representative image      Extracts a representative image (i.e, featured image)
 
 RMD Reader                Create posts via knitr RMarkdown files
 
+Section number            Adds section number to the article's context, in the form of `2.3.3` Sections are indicated by `<h1>-<h6>` in the parsed html format.
+
 Share post                Creates share URLs of article
 
 Simple footnotes          Adds footnotes to blog posts

+ 39 - 0
section_number/Readme.md

@@ -0,0 +1,39 @@
+Section number
+---------------
+
+This plugin adds section number to the article's context, in the form of `X.X.X`. Sections are indicated by `<h1>-<h6>` in the parsed html format. 
+
+# Setting
+
+By default, up to 3 levels of sections are numbered. You can customize this value by defining `SECTION_NUMBER_MAX` in your setting file:
+
+```
+SECTION_NUMBER_MAX = 5
+```
+
+# caution
+
+The first section in the article will be marked as the top section level. Namely, if `<h3>` is the first encountered section, no `<h1>` or `<h2>` is supposed to exist. Else, exception may be thrown out.
+
+# Example
+the following markdown content:
+```
+# section
+blabla
+## subsection
+blabla
+## subsection
+blabla
+# section
+blabla
+```
+will be
+
+>#1 section
+>blabla
+>##1.1 subsection
+>blabla
+>##1.2 subsection
+>blabla
+>#2 section
+>blabla

+ 1 - 0
section_number/__init__.py

@@ -0,0 +1 @@
+from .section_number import *

+ 89 - 0
section_number/section_number.py

@@ -0,0 +1,89 @@
+"""
+Section number plugin for Pelican
+================================
+Adds section numbers to section titles of the article
+"""
+
+from pelican import signals, contents
+
+
+def _extract_level(text, idx):
+    end = text.find(">", idx)
+
+    if end == -1:
+        return (idx, -1)
+
+    try:
+        level = int(text[idx : end])
+        return (end, level)
+
+    except:
+        return (idx, -1)
+
+
+def _level_str(level_nums, level_max):
+    ret = u''
+
+    if len(level_nums) > level_max:
+        return ret
+
+    for n in level_nums:
+        ret += str(n) + '.'
+
+    return ret[:-1] + ' '
+
+
+def _insert_title_number(text, level_max):
+    ret = u''
+    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):
+    if content._content is None:
+        return
+
+    level_max = content.settings.get('SECTION_NUMBER_MAX', 3)
+    
+    if level_max <= 0:
+        return
+
+    content._content = _insert_title_number(content._content, level_max)
+
+
+def register():
+    signals.content_object_init.connect(process_content)