123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- """
- Better Code-Block Line Numbering Plugin
- --------------------------
- Authored by Jacob Levernier, 2014
- Released under the BSD 2-Clause License (http://opensource.org/licenses/BSD-2-Clause)
- For more information on this plugin, please see the attached Readme.md file.
- """
- from pelican import signals
- import os.path
- import re
- def add_line_wrappers(data_passed_from_pelican):
- """A function to read through each page and post as it comes through from Pelican, find all instances of triple-backtick (```...```) code blocks, and add an HTML wrapper to each line of each of those code blocks"""
- if data_passed_from_pelican._content:
- full_content_of_page_or_post = data_passed_from_pelican._content
- else:
- return
- all_instances_of_pre_elements = re.findall('<pre>.*?</pre>', full_content_of_page_or_post, re.DOTALL)
-
- if(len(all_instances_of_pre_elements) > 0):
- updated_full_content_of_page_or_post = full_content_of_page_or_post
-
- for pre_element_to_parse in all_instances_of_pre_elements:
-
-
- replacement_text_with_beginning_of_each_line_wrapped_in_span = re.sub(r'(<pre.*?>|\n(?!</pre>))','\\1<span class="code-line">',pre_element_to_parse)
-
- replacement_text_with_full_line_wrapped_in_span = re.sub(r'((?<!</pre>)$|(?<!</pre>)\n)','</span>\\1',replacement_text_with_beginning_of_each_line_wrapped_in_span)
-
- updated_full_content_of_page_or_post = updated_full_content_of_page_or_post.replace(pre_element_to_parse,replacement_text_with_full_line_wrapped_in_span)
-
- data_passed_from_pelican._content = updated_full_content_of_page_or_post
- def register():
- signals.content_object_init.connect(add_line_wrappers)
|