Skip to content

Commit 744dff8

Browse files
committed
Add support for left, center, right and justified alignment, ignore tags in comments
1 parent 117da11 commit 744dff8

File tree

3 files changed

+69
-11
lines changed

3 files changed

+69
-11
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ For a syntax- and feature-overview, have a look at [Quick Reference](#quick-refe
77

88
# Markup
99

10-
Version 1
10+
Version 2
1111

1212
> [!NOTE]
1313
> See `example.mint` for a working usage example.
@@ -31,6 +31,10 @@ Tags toggle a style, so you can start bold text with `/b/` and end it with `/b/`
3131
| `/u/.../u/` | `<u>...</u>` | <u>Underlined text</u> |
3232
| `/d/.../d/` | `<s>...</s>` | ~~Deleted text~~ |
3333
| `/e/.../e/` | `<pre>...</pre>` | Keep whitespaces (spaces, new lines, ...) |
34+
| `/</...` | `<section style='text-align: left'>...` | Switch to left text-alignment |
35+
| `/\|/...` | `<section style='text-align: center'>...` | Switch to center text-alignment |
36+
| `/>/...` | `<section style='text-align: right'>...` | Switch to right text-alignment |
37+
| `/=/...` | `<section style='text-align: justify'>...` | Switch to justified text-alignment |
3438
| `/l/` | `<br>` | Line-break (new line) |
3539
| `//` | `/` | Escape a forward slash |
3640

example.mint

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,64 @@
33

44
/#/ Example comment /#/
55

6+
7+
Per default, text is aligned left
8+
/|/
9+
but you can also center it
10+
/>/
11+
or align it to the right.
12+
13+
/#/ put a newline: /#/
14+
15+
/</ /l/ Remember to switch back to left-alignment
16+
17+
/l/
18+
19+
Text can also be aligned justified: /=/
20+
621
/p/
722
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
823
/p/
924

25+
/</
26+
1027
/s/A Subheading/s/
1128

1229
Mint supports /#/ inline comments and/#/ /b/bold/b/, /i/italic/i/ and /u/underlined/u/ text.
1330

14-
/#/ put a newline: /#/ /l/
15-
1631
You can also /d/strike-through/d/ text to indicate that it has been deleted.
1732

1833
/u//i/Obviously/i/, you can /b/mix/b//u/ styles.
1934

2035
/p/
2136
Show off a quote:
2237

38+
/#/ NOTE: if you want to align a blockquote, put your alignment tag before the /q//#/
39+
2340
/q/
2441
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Temporibus autem quibusdam et aut officiis debitis aut rerum necessitatibus saepe eveniet ut et voluptates repudiandae sint et molestiae non recusandae. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
25-
/l//l/
26-
/i/- "de Finibus Bonorum et Malorum", Cicero/i/
42+
/l/
43+
/|//i/"de Finibus Bonorum et Malorum", Cicero/i/
2744
/q/
2845
/p/
2946

47+
/</
48+
3049
/s/Another Subheading/s/
3150

3251
Enforce a // with "////" to get //u//, //i//, etc.
3352
or an url like https:////example.org
3453

35-
/e/You can also have sections where whitespaces are kept as-is.
54+
/e/You can also have "pre-formatted" sections where whitespaces are kept as-is.
3655
... which means you can do stuff like
3756
/u/this/u/.
3857
/e/
3958

59+
/=/
60+
4061
/p/
4162
Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?
4263
/p/
64+
65+
/>/
66+
bye :)

mint2html.py

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class ConverterState:
5353
underline = False
5454
strike_through = False
5555
whitespace_pre = False
56+
alignment = "" # left: < center: | right > justify: =
5657

5758
def complete_cycle(self):
5859
self.pos += self.pos_offset
@@ -82,6 +83,11 @@ class MintToHtmlConverter:
8283
- underline /u/
8384
- deleted (strike-through) /d/
8485
86+
- align to left /</
87+
- align to center /|/
88+
- align to right />/
89+
- align justified /=/
90+
8591
- keep whitespaces /e/
8692
- line breaks /l/
8793
'''
@@ -93,8 +99,9 @@ def __init__(
9399
):
94100
self.escape_html = escape_html
95101
self.css = css
102+
self.comment_tag = MintTagToHtml("#", None, "comment")
96103
self.tags = [
97-
MintTagToHtml("#", None, "comment"),
104+
self.comment_tag,
98105
MintTagToHtml("p", "p", "paragraph"),
99106
MintTagToHtml("q", "blockquote", "blockquote"),
100107
MintTagToHtml("h", "h1", "heading"),
@@ -106,14 +113,24 @@ def __init__(
106113
MintTagToHtml("e", "pre", "whitespace_pre"),
107114
MintTagToHtml("l", "br", None)
108115
]
116+
self.alignments = {
117+
"<": "left",
118+
"|": "center",
119+
">": "right",
120+
"=": "justify"
121+
}
109122

110123
def __call__(self, text_in: str, ) -> str:
111124
'''Convert mint to html'''
112125
# replace unwanted characters
113126
text_in = text_in.replace("\r", "")
114127
# html escape
128+
text_in = text_in.replace("/</", "/lalgn/")
129+
text_in = text_in.replace("/>/", "/ralgn/")
115130
if self.escape_html:
116131
text_in = escape(text_in)
132+
text_in = text_in.replace("/lalgn/", "/</")
133+
text_in = text_in.replace("/ralgn/", "/>/")
117134
# parsing & conversion
118135
state = ConverterState()
119136

@@ -149,7 +166,7 @@ def process_inline_tag(c1: str, state: ConverterState, t: MintTagToHtml):
149166
break
150167
c1 = text_in[state.pos + 1]
151168
if c == TAG_BOUNDARY:
152-
if c1 == TAG_BOUNDARY:
169+
if c1 == TAG_BOUNDARY and not state.comment:
153170
# e.g. // -> /
154171
state.pos_offset += 1
155172
state.add_output(TAG_BOUNDARY)
@@ -158,14 +175,27 @@ def process_inline_tag(c1: str, state: ConverterState, t: MintTagToHtml):
158175
if state.pos + 2 < len_text_in:
159176
c2 = text_in[state.pos + 2]
160177
if c2 == TAG_BOUNDARY:
161-
# process tags
162-
for t in self.tags:
163-
process_inline_tag(c1, state, t)
178+
if state.comment:
179+
process_inline_tag(c1, state, self.comment_tag)
180+
else:
181+
for t in self.tags:
182+
process_inline_tag(c1, state, t)
183+
# toggle alignment
184+
if c1 in self.alignments:
185+
state.pos_offset += 2
186+
if state.alignment == c1:
187+
pass
188+
elif state.alignment != "":
189+
state.add_output("</section>")
190+
state.add_output(f"<section style='text-align: {self.alignments[c1]}'>")
191+
state.alignment = c1
164192
if not state.cycle_had_op and not state.comment:
165193
state.output += c
166194
# complete this cycle's state
167195
state.complete_cycle()
168196
# cleanup
197+
if state.alignment != "":
198+
state.add_output("</section>")
169199
state.output = state.output.strip()
170200
# return result
171201
return state.output

0 commit comments

Comments
 (0)