-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added automatic html section creation tool
- Loading branch information
Showing
3 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import re | ||
import markdown | ||
from markdown.extensions import Extension | ||
from markdown.preprocessors import Preprocessor | ||
|
||
|
||
class CustomSectionExtension(Extension): | ||
def extendMarkdown(self, md): | ||
md.preprocessors.register(CustomSectionPreprocessor(md), 'custom_section', 175) | ||
|
||
|
||
class CustomSectionPreprocessor(Preprocessor): | ||
def run(self, lines): | ||
new_lines = [] | ||
in_section = False | ||
header_content = None | ||
for line in lines: | ||
header_match = re.match(r'^\s*#{1,6}\s+(.*)', line) | ||
if header_match: | ||
if in_section: | ||
new_lines.append('</div>') # Close the previous section | ||
new_lines.append('</section>') # Close the previous section | ||
header_content = header_match.group(1).strip() | ||
header_id = header_content.lower().replace(' ', '-') | ||
new_lines.append(f'<section id="{header_id}">') | ||
new_lines.append('<div class="section">') | ||
new_lines.append(f'<h2>{header_content}</h2>') | ||
in_section = True | ||
elif in_section: | ||
if line.strip() == "": | ||
continue # Skip empty lines | ||
if line.strip().startswith("!["): | ||
# Extracting image source and alt text | ||
img_src_match = re.search(r'\((.*?)\)', line) | ||
if img_src_match: | ||
img_src = img_src_match.group(1) | ||
alt_text_match = re.search(r'\[(.*?)\]', line) | ||
alt_text = alt_text_match.group(1) if alt_text_match else "" | ||
new_lines.append(f'<img src="{img_src}" alt="{alt_text}">') | ||
elif line.strip().startswith("["): | ||
# Extracting link text and URL | ||
link_text_match = re.search(r'\[(.*?)\]', line) | ||
link_text = link_text_match.group(1) if link_text_match else "" | ||
link_url_match = re.search(r'\((.*?)\)', line) | ||
link_url = link_url_match.group(1) if link_url_match else "" | ||
new_lines.append(f'<a href="{link_url}">{link_text}</a><br>') | ||
elif line.strip().startswith("```"): | ||
new_lines.append(line) # Preserve code blocks | ||
else: | ||
new_lines.append(f'<p>{line.strip()}</p>') | ||
if in_section: | ||
new_lines.append('</div>') # Close the last section | ||
new_lines.append('</section>') # Close the last section | ||
return new_lines | ||
|
||
|
||
def markdown_to_html(input_file, output_file): | ||
with open(input_file, 'r', encoding='utf-8') as f: | ||
text = f.read() | ||
md = markdown.Markdown(extensions=[CustomSectionExtension(), 'markdown.extensions.attr_list']) | ||
html = md.convert(text) | ||
with open(output_file, 'w', encoding='utf-8') as out: | ||
out.write(html) | ||
|
||
if __name__ == "__main__": | ||
# input_file = input("Enter input Markdown file path: ") | ||
# output_file = input("Enter output HTML file path: ") | ||
markdown_to_html("README.md", "output.html") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<section id="bq76942-pcbs-esp32"> | ||
<div class="section"> | ||
<h2>BQ76942-pcbs-esp32</h2> | ||
<p>a repo that contains test PCBs and code for the BQ76942 chip.</p> | ||
<a href="https://easyeda.com/editor#id=f5f4d9792d09474ea4b0e3cc912bd76b|1c698f998313432a857b05c3b1bb7821">Easyeda | ||
Project</a><br> | ||
<img src="documents/img/BQ76942-test-board-V1.0-image-3D-front.PNG" alt="lol"> | ||
<p><a href="https://www.buymeacoffee.com/georgmareku" target="_blank"><img | ||
src="https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png" alt="Buy Me A Coffee" | ||
style="height: 41px !important;width: 174px !important;box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;-webkit-box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;"></a> | ||
</p> | ||
</div> | ||
</section> | ||
<section id="test123"> | ||
<div class="section"> | ||
<h2>test123</h2> | ||
<p>this is very good content lol</p> | ||
<img src="documents/img/BQ76942-diagram.drawioBig.png" alt="no image"> | ||
</div> | ||
</section> |