summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristoph Schlosser <christoph@linux.com>2025-10-10 17:19:27 +0200
committerChristoph Schlosser <christoph@linux.com>2025-10-10 17:19:27 +0200
commit85da4abc206f2cc8819836bb0e8727de3a51d24c (patch)
tree6c4884cef49da8ccb1f21bc180d4b6a5681bd267
parent2a076517255c4585ac2bc475aaafdc728b6dae6f (diff)
downloadgit2hugo-85da4abc206f2cc8819836bb0e8727de3a51d24c.tar.gz
g2h: Render markdown files instead of putting them into code blocks
-rw-r--r--README.md3
-rw-r--r--src/g2h/cli.py2
-rw-r--r--src/g2h/file_page.py7
-rw-r--r--templates/file.md4
-rw-r--r--tests/file_page_test.py12
5 files changed, 24 insertions, 4 deletions
diff --git a/README.md b/README.md
index c908e1d..ee32164 100644
--- a/README.md
+++ b/README.md
@@ -59,7 +59,7 @@ For a full list of options, their defaults and how they're changing the behavior
- [x] Don't treat pictures as binary files.
- [x] Breadcrumbs for navigating.
- [x] Escape markdown backticks in plaintext files.
- - [ ] Render markdown files.
+ - [x] Render markdown files.
- [ ] Add button to download raw file.
- [x] Nav bar for navigating the pages mentioned above.
- [ ] Link to special readme, if a readme can be found, rendering markdown.
@@ -69,6 +69,7 @@ For a full list of options, their defaults and how they're changing the behavior
- [ ] Parallelize generating output.
- [ ] Rename directories with a leading dot.
- [ ] Fix filenames that contain markdown formatting, like Pythons `__init__.py`.
+- [ ] Generate branch, tag aliases for commits so one can link to them directly without having to update the commit hash.
## Similar projects
diff --git a/src/g2h/cli.py b/src/g2h/cli.py
index fe3850c..bc59467 100644
--- a/src/g2h/cli.py
+++ b/src/g2h/cli.py
@@ -11,7 +11,7 @@ def parse_args():
parser.add_argument('--repo', help='Path to the root of git repository', default=os.getcwd())
parser.add_argument('--out', help='Path to where the generated files should be stored', default=os.getcwd())
# TODO: Add an actual default value.
- parser.add_argument('--name', help='Name of the project. Default: directory name of repo')
+ parser.add_argument('--name', help='Name of the project. Default: directory name of repo (TODO)')
parser.add_argument('--content-subdir', help='Subdir(s) where the page will be served from. Default: None', default='')
return parser.parse_args()
diff --git a/src/g2h/file_page.py b/src/g2h/file_page.py
index d64cb84..f2e0550 100644
--- a/src/g2h/file_page.py
+++ b/src/g2h/file_page.py
@@ -38,8 +38,11 @@ class FilePage(Page):
content, file_type = bytes_to_content(file_bytes)
backtick_max = 2
if file_type == 'text':
- matches = re.findall('`+', content)
- backtick_max = max(max(len(match) for match in matches) if matches else 0, backtick_max)
+ if self.file_name.lower().endswith('.md'):
+ backtick_max = -1
+ else:
+ matches = re.findall('`+', content)
+ backtick_max = max(max(len(match) for match in matches) if matches else 0, backtick_max)
return {
'backtick_max': backtick_max + 1,
diff --git a/templates/file.md b/templates/file.md
index 5f4dcbd..39b3202 100644
--- a/templates/file.md
+++ b/templates/file.md
@@ -6,10 +6,14 @@
{% include '_breadcrumbs.md' %}
{% if type == 'text' %}
+{% if backtick_max >= 3 %}
{% set backticks = '`' * backtick_max %}
{{ backticks }}{{ file_extension }}
+{% endif %}
{{ content }}
+{% if backtick_max >= 3 %}
{{ backticks }}
+{% endif %}
{% elif type == 'image' %}
![](data:image/png;base64,{{ content }})
{% elif type == 'empty' %}
diff --git a/tests/file_page_test.py b/tests/file_page_test.py
index daa9b6c..5c380b8 100644
--- a/tests/file_page_test.py
+++ b/tests/file_page_test.py
@@ -94,3 +94,15 @@ def test_backticks_in_text_content():
file.data_stream.read.return_value = '```'.encode()
context = page.context()
assert(context['backtick_max'] == 4)
+
+def test_markdown():
+ file = MagicMock()
+ tree = MagicMock()
+ commit = MagicMock()
+ commit.hexsha = '123'
+ commit.tree = tree
+ commit.tree.join.return_value = file
+ page = FilePage('test/file', 'file test', commit, 'file.Md', '', 'file/content')
+ file.data_stream.read.return_value = '```'.encode()
+ context = page.context()
+ assert(context['backtick_max'] == 0)