Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ea20c76

Browse files
committed
Support display sup/sub styles of numbers
1 parent faf2169 commit ea20c76

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

β€Žsrc/helper.rs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ mod html {
9898
pub enum Token {
9999
Plain(String),
100100
Bold(String),
101+
Sup(String),
102+
Sub(String),
101103
Eof(String),
102104
}
103105

@@ -107,6 +109,61 @@ mod html {
107109
fn render(&self) -> String;
108110
}
109111

112+
pub fn superscript(n: u8) -> String {
113+
if n == 0 {
114+
"0".to_owned()
115+
} else if n == 1 {
116+
"1".to_owned()
117+
} else if n == 2 {
118+
"2".to_owned()
119+
} else if n == 3 {
120+
"3".to_owned()
121+
} else if n == 4 {
122+
"4".to_owned()
123+
} else if n == 5 {
124+
"5".to_owned()
125+
} else if n == 6 {
126+
"6".to_owned()
127+
} else if n == 7 {
128+
"7".to_owned()
129+
} else if n == 8 {
130+
"8".to_owned()
131+
} else if n == 9 {
132+
"9".to_owned()
133+
} else if n >= 10 {
134+
superscript(n / 10) + &superscript(n % 10)
135+
} else {
136+
n.to_string()
137+
}
138+
}
139+
140+
pub fn subscript(n: u8) -> String {
141+
if n >= 10 {
142+
subscript(n / 10) + &subscript(n % 10)
143+
} else if n == 0 {
144+
"0".to_owned()
145+
} else if n == 1 {
146+
"1".to_owned()
147+
} else if n == 2 {
148+
"2".to_owned()
149+
} else if n == 3 {
150+
"3".to_owned()
151+
} else if n == 4 {
152+
"4".to_owned()
153+
} else if n == 5 {
154+
"5".to_owned()
155+
} else if n == 6 {
156+
"6".to_owned()
157+
} else if n == 7 {
158+
"7".to_owned()
159+
} else if n == 8 {
160+
"8".to_owned()
161+
} else if n == 9 {
162+
"9".to_owned()
163+
} else {
164+
n.to_string()
165+
}
166+
}
110167
impl HTML for String {
111168
fn ser(&self) -> Vec<Token> {
112169
// empty tags
@@ -117,12 +174,20 @@ mod html {
117174
let mut ptr = 0;
118175
let mut output = vec![];
119176
let mut bold = false;
177+
let mut sup = false;
178+
let mut sub = false;
120179
for (i, e) in tks.chars().enumerate() {
121180
match e {
122181
'<' => {
123182
if bold {
124183
output.push(Token::Bold(tks[ptr..i].to_string()));
125184
bold = false;
185+
} else if sup {
186+
output.push(Token::Sup(tks[ptr..i].to_string()));
187+
sup = false;
188+
} else if sub {
189+
output.push(Token::Sub(tks[ptr..i].to_string()));
190+
sub = false;
126191
} else {
127192
output.push(Token::Plain(tks[ptr..i].to_string()));
128193
}
@@ -133,6 +198,8 @@ mod html {
133198
"-" => continue,
134199
_ => match &tks[(ptr + 1)..i] {
135200
"b" | "strong" => bold = true,
201+
"sup" => sup = true,
202+
"sub" => sub = true,
136203
_ => {}
137204
},
138205
}
@@ -168,6 +235,14 @@ mod html {
168235

169236
tks.push(s.bold().to_string());
170237
}
238+
Token::Sup(s) => tks.push(match s.parse::<u8>() {
239+
Ok(n) => superscript(n),
240+
_ => s,
241+
}),
242+
Token::Sub(s) => tks.push(match s.parse::<u8>() {
243+
Ok(n) => subscript(n),
244+
_ => s,
245+
}),
171246
Token::Eof(s) => tks.push(s.normal().to_string()),
172247
}
173248
}

0 commit comments

Comments
(0)

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /