Skip to content

Commit

Permalink
Remove the common leading whitespace from each line in the code block.
Browse files Browse the repository at this point in the history
  • Loading branch information
yym68686 committed Jun 17, 2024
1 parent 2556db2 commit 52bc919
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

setup(
name="md2tgmd",
version="0.2.2",
version="0.2.3",
description="md2tgmd is a Markdown to Telegram-specific-markdown converter.",
long_description=Path.open(Path("README.md"), encoding="utf-8").read(),
long_description_content_type="text/markdown",
Expand Down
17 changes: 11 additions & 6 deletions src/md2tgmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ def escapeplus(text):
def escape_all_backquote(text):
return '\\' + text

def dedent_space(text):
import textwrap
return textwrap.dedent(text)

def find_lines_with_char(s, char, min_count):
"""
返回字符串中每行包含特定字符至少min_count次的行的索引列表。
Expand Down Expand Up @@ -134,6 +138,7 @@ def escape(text, flag=0):
text = re.sub(r"\.", '\.', text)
text = re.sub(r"!", '\!', text)
text = find_lines_with_char(text, '`', 5)
text = replace_all(text, r"(\x20*```[\D\d\s]+?```)", dedent_space)
return text

text = r'''
Expand Down Expand Up @@ -184,12 +189,12 @@ def escape(text, flag=0):
And simple text `with-ten` `with+ten` + some - **symbols**. # `with-ten`里面的`-`不会被转义
```
print("Hello, World!") -
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
```
```
print("Hello, World!") -
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
```
Cxy = abs (Pxy)**2/ (Pxx*Pyy)
Expand Down
15 changes: 15 additions & 0 deletions test/test_dedent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import textwrap

def example():
text = """
```
print("Hello, World!") -
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
```
"""
# text = textwrap.dedent(text)
print(text)

example()

0 comments on commit 52bc919

Please sign in to comment.