1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
use std::collections::HashMap;
use std::fs;
use std::io::Write;
use fancy_regex::Regex;
use crate::highlight::{format_language, format_pyint, get_hl};
use crate::text::{
allreplace, br, hb_format, mathjax_eq_resub, prepend_nbsps, re_replace_all,
remove_trailing_comment,
};
/// The main jemdoc parser and processor.
pub struct JemdocParser {
/// All input lines (each ending with \n).
pub lines: Vec<String>,
/// Current position (line index).
pub pos: usize,
/// Current line number (for error messages).
pub linenum: usize,
/// Output writer.
pub outf: Box<dyn Write>,
/// Configuration map (section tag -> HTML content).
pub conf: HashMap<String, String>,
/// Input filename.
pub inname: String,
/// Whether equations are enabled.
pub eqs: bool,
/// Google Analytics tracking ID.
pub analytics: Option<String>,
/// Current table row number.
pub tablerow: usize,
/// Current table column number.
pub tablecol: usize,
/// File inclusion stack: (lines, pos, linenum).
pub file_stack: Vec<(Vec<String>, usize, usize)>,
}
impl JemdocParser {
/// Create a new JemdocParser.
pub fn new(
inname: String,
lines: Vec<String>,
outf: Box<dyn Write>,
conf: HashMap<String, String>,
) -> Self {
JemdocParser {
lines,
pos: 0,
linenum: 0,
outf,
conf,
inname,
eqs: true,
analytics: None,
tablerow: 0,
tablecol: 0,
file_stack: Vec::new(),
}
}
// =========================================================================
// Output helpers
// =========================================================================
/// Write a string to the output.
pub fn out(&mut self, s: &str) {
let _ = self.outf.write_all(s.as_bytes());
}
/// Write a half-block to the output.
pub fn hb(
&mut self,
tag: &str,
content1: &str,
content2: Option<&str>,
content3: Option<&str>,
) {
let s = hb_format(tag, content1, content2, content3);
self.out(&s);
}
// =========================================================================
// File inclusion
// =========================================================================
/// Push the current file onto the stack and switch to a new include file.
pub fn push_file(&mut self, filename: &str) {
let old_lines = std::mem::take(&mut self.lines);
let old_pos = self.pos;
let old_linenum = self.linenum;
self.file_stack.push((old_lines, old_pos, old_linenum));
let content = fs::read_to_string(filename).unwrap_or_default();
self.lines = content.lines().map(|l| format!("{}\n", l)).collect();
self.pos = 0;
self.linenum = 0;
}
/// Pop back to the previous file from the stack.
pub fn next_file(&mut self) {
if let Some((lines, pos, linenum)) = self.file_stack.pop() {
self.lines = lines;
self.pos = pos;
self.linenum = linenum;
}
}
/// Get a config section value, or empty string if not found.
fn conf(&self, key: &str) -> String {
self.conf.get(key).cloned().unwrap_or_default()
}
/// Process a potential include directive. Returns true if it was an include.
pub fn do_includes(&mut self, l: &str) -> bool {
let l = l.trim();
if l.starts_with("includeraw{") && l.ends_with('}') {
let filename = &l["includeraw{".len()..l.len() - 1];
if let Ok(content) = fs::read_to_string(filename) {
self.out(&content);
}
return true;
} else if l.starts_with("include{") && l.ends_with('}') {
let filename = &l["include{".len()..l.len() - 1];
self.push_file(filename);
return true;
}
false
}
// =========================================================================
// Input reading primitives
// =========================================================================
/// Peek at the next meaningful character without consuming non-comment lines.
/// Comment lines ARE consumed and skipped.
pub fn pc(&mut self, ditch_comments: bool) -> String {
loop {
if self.pos >= self.lines.len() {
if !self.file_stack.is_empty() {
self.next_file();
continue;
}
return String::new(); // EOF
}
let line = self.lines[self.pos].clone();
let trimmed = line.trim_start_matches([' ', '\t']);
// Empty line
if trimmed.is_empty() || trimmed == "\n" {
return "\n".to_string();
}
let first_char = match trimmed.chars().next() {
Some(c) => c,
None => return "\n".to_string(),
};
// Handle comments
if ditch_comments && first_char == '#' {
let comment_content = &trimmed[1..].trim();
self.pos += 1;
self.linenum += 1;
if self.do_includes(comment_content) {
return "#".to_string();
}
// Skip this comment line
continue;
}
// Handle backslash-prefixed tokens like \( and \)
if first_char == '\\' && trimmed.len() > 1 {
let second = trimmed.chars().nth(1).unwrap();
return format!("\\{}", second);
}
return first_char.to_string();
}
}
/// Get the next input line.
/// If withcount is true, also returns the count of leading identical characters.
/// If codemode is true, returns the raw line without stripping.
pub fn nl(&mut self, withcount: bool, codemode: bool) -> Option<(String, usize)> {
if self.pos >= self.lines.len() {
if !self.file_stack.is_empty() {
self.next_file();
return self.nl(withcount, codemode);
}
return None;
}
let mut s = self.lines[self.pos].clone();
self.pos += 1;
self.linenum += 1;
if codemode {
// Return raw line
return Some((s, 0));
}
// Strip leading whitespace
s = s.trim_start_matches([' ', '\t']).to_string();
// Remove trailing comments
let trimmed = remove_trailing_comment(&s);
s = format!("{}\n", trimmed);
let count = if withcount && s.len() > 1 {
let first_char = s.chars().next().unwrap_or('\n');
if first_char == '\n' {
0
} else {
s.chars().take_while(|&c| c == first_char).count()
}
} else {
0
};
// Strip leading marker characters (-, ., =, :)
s = s.trim_start_matches(['-', '.', '=', ':']).to_string();
Some((s, count))
}
/// Get the next paragraph from the input file.
/// Reads lines until a paragraph-break signal is encountered.
pub fn np(&mut self, withcount: bool, eatblanks: bool) -> Option<(String, usize)> {
let (mut s, c) = self.nl(withcount, false)?;
// Detect open inline equation blocks
let dollar_re = Regex::new(r"(?<!\\)\$").unwrap();
let match_count = dollar_re.find_iter(&s).count();
let mut lm = match_count % 2;
let mut is_open_eq = lm == 1;
// Characters that signal a new paragraph
let nl_signals = ["\n", ".", ":", "", "=", "~", "{", "\\(", "\\)"];
loop {
let pcf = self.pc(true);
if pcf.is_empty() {
break;
}
let is_signal = nl_signals.contains(&pcf.as_str());
if !is_open_eq {
// Not in open equation: break on - or nl_signals
if pcf == "-" || is_signal {
break;
}
} else {
// In open equation: break only on nl_signals (allow -)
if is_signal {
break;
}
if pcf == "-" {
s.push('-');
}
}
let ns = match self.nl(false, false) {
Some((line, _)) => line,
None => break,
};
let ns_matches = dollar_re.find_iter(&ns).count();
lm = (lm + ns_matches) % 2;
is_open_eq = lm == 1;
s.push_str(&ns);
}
// Eat blank lines
if eatblanks {
while self.pc(true) == "\n" {
self.nl(false, false); // burn blank line
}
}
Some((s, c))
}
// =========================================================================
// Block replacement (calls text::br with parser state)
// =========================================================================
/// Perform block replacements using parser state.
pub fn br(&mut self, b: &str, tableblock: bool) -> String {
br(b, self.eqs, tableblock, &mut self.tablerow)
}
// =========================================================================
// List processing
// =========================================================================
/// Process a dash-list (unordered) or dot-list (ordered).
pub fn dashlist(&mut self, ordered: bool) {
let mut level = 0usize;
let (char_marker, ul_tag) = if ordered { (".", "ol") } else { ("-", "ul") };
while self.pc(true) == char_marker {
let (s, newlevel) = match self.np(true, false) {
Some(v) => v,
None => break,
};
if newlevel > level {
for _ in 0..(newlevel - level) {
if newlevel > 1 {
self.out("\n");
}
self.out(&format!("<{}>\n<li>", ul_tag));
}
} else if newlevel < level {
self.out("\n</li>");
for _ in 0..(level - newlevel) {
self.out(&format!("</{}>\n</li>", ul_tag));
}
self.out("\n<li>");
} else {
self.out("\n</li>\n<li>");
}
let processed = self.br(&s, false);
let processed = mathjax_eq_resub(&processed);
self.out(&format!("<p>{}</p>", processed));
level = newlevel;
}
for _ in 0..level {
self.out(&format!("\n</li>\n</{}>\n", ul_tag));
}
}
/// Process a colon-list (definition list).
pub fn colonlist(&mut self) {
let re = Regex::new(r"(?ms)\s*\{(.*?)(?<!\\)\}(.*)").unwrap();
self.out("<dl>\n");
while self.pc(true) == ":" {
let (s, _) = match self.np(false, false) {
Some(v) => v,
None => break,
};
if let Ok(Some(caps)) = re.captures(&s) {
let defpart = caps.get(1).map(|m| m.as_str()).unwrap_or("");
let rest = caps.get(2).map(|m| m.as_str()).unwrap_or("");
let def_html = self.br(defpart, false);
self.hb("<dt>|</dt>\n", &def_html, None, None);
let rest_html = self.br(rest, false);
self.hb("<dd><p>|</p></dd>\n", &rest_html, None, None);
}
}
self.out("</dl>\n");
}
// =========================================================================
// Code block processing
// =========================================================================
/// Process a code block (delimited by ~).
pub fn codeblock(&mut self, title: Option<&str>, lang: &str) {
if lang == "raw" {
// Raw mode: output verbatim
while let Some((line, _)) = self.nl(false, true) {
if line.starts_with('~') {
break;
}
let line = if line.starts_with("\\~") {
line[1..].to_string()
} else {
line
};
self.out(&line);
}
return;
}
if title == Some("filter_through") {
// Filter through external program (simplified - just output raw).
// In the Python version, lang is the external command name.
let mut buff = String::new();
while let Some((line, _)) = self.nl(false, true) {
if line.starts_with('~') {
break;
}
buff.push_str(&line);
}
// In the Python version this pipes through an external program.
// For now we just output the raw buffer.
self.out(&buff);
return;
}
// Normal code block
let codeblock_conf = self.conf("codeblock");
let blocktitle_conf = self.conf("blocktitle");
let codeblockcontenttt_conf = self.conf("codeblockcontenttt");
let codeblockcontent_conf = self.conf("codeblockcontent");
let codeblockendtt_conf = self.conf("codeblockendtt");
let codeblockend_conf = self.conf("codeblockend");
self.out(&codeblock_conf);
if let Some(t) = title {
if !t.is_empty() {
self.hb(&blocktitle_conf, t, None, None);
}
}
if lang == "jemdoc" {
self.out(&codeblockcontenttt_conf);
} else {
self.out(&codeblockcontent_conf);
}
let mut stringmode = false;
let mut first_line = true;
while let Some((line, _)) = self.nl(false, true) {
if line.starts_with('~') {
break;
}
let line = if line.starts_with("\\~") || line.starts_with("\\{") {
line[1..].to_string()
} else {
line
};
if stringmode {
if line.trim_end().ends_with("\"\"\"") {
self.out(&format!("{}</span>", line));
stringmode = false;
} else {
self.out(&line);
}
continue;
}
if lang == "pyint" {
let formatted = format_pyint(&line);
self.out(&formatted);
first_line = false;
} else if lang == "jemdoc" {
let ltrimmed = line.trim_start();
let special_starts = ["#", "~", ">>>", "\\~"];
let mut handled = false;
for prefix in &special_starts {
if ltrimmed.starts_with(prefix) {
self.out("</tt><pre class=\"tthl\">");
self.out(&line);
self.out("</pre><tt class=\"tthl\">");
handled = true;
break;
}
}
if !handled {
let colon_starts = [":", ".", "-"];
let mut handled2 = false;
for prefix in &colon_starts {
if ltrimmed.starts_with(prefix) {
let prefix_str = if first_line { "" } else { "<br />" };
self.out(&format!("{}{}", prefix_str, prepend_nbsps(&line)));
handled2 = true;
break;
}
}
if !handled2 {
if ltrimmed.starts_with('=') {
self.out(&format!("{}<br />", prepend_nbsps(&line)));
} else if ltrimmed.is_empty() {
self.out(&line);
} else {
let prefix_str = if first_line { "" } else { "<br />" };
self.out(&format!("{}{}", prefix_str, line));
}
}
first_line = false;
} else {
first_line = true;
}
} else {
// Check for includes in code blocks
if line.starts_with("\\#include{") || line.starts_with("\\#includeraw{") {
self.out(&line[1..]);
} else if line.starts_with('#') && self.do_includes(&line[1..]) {
continue;
} else if (lang == "python" || lang == "py") && line.trim().starts_with("\"\"\"") {
self.out(&format!("<span class=\"string\">{}", line));
stringmode = true;
} else {
let hl = get_hl(lang);
let formatted = format_language(&line, &hl);
self.out(&formatted);
}
first_line = false;
}
}
if lang == "jemdoc" {
self.out(&codeblockendtt_conf);
} else {
self.out(&codeblockend_conf);
}
}
// =========================================================================
// Title insertion
// =========================================================================
/// Insert the document title.
pub fn insert_title(&mut self, title: Option<&str>) {
if let Some(t) = title {
let doctitle_conf = self.conf("doctitle");
self.hb(&doctitle_conf, t, None, None);
// Look for a subtitle
if self.pc(true) != "\n" {
let (subtitle_text, _) = self.np(false, true).unwrap_or_default();
let processed = self.br(&subtitle_text, false);
let subtitle_conf = self.conf("subtitle");
self.hb(&subtitle_conf, &processed, None, None);
}
let doctitleend_conf = self.conf("doctitleend");
self.hb(&doctitleend_conf, t, None, None);
}
}
// =========================================================================
// Menu insertion
// =========================================================================
/// Insert menu items from a MENU file.
pub fn insert_menu_items(&mut self, mname: &str, current: &str, prefix: &str) {
// Resolve the MENU file path:
// 1. Try the path as given (relative to CWD).
// 2. Fall back to resolving relative to the input file's directory.
let menu_path =
if std::path::Path::new(mname).is_absolute() || std::path::Path::new(mname).exists() {
std::path::PathBuf::from(mname)
} else {
let indir = std::path::Path::new(&self.inname)
.parent()
.unwrap_or_else(|| std::path::Path::new("."));
indir.join(mname)
};
let menu_content = match fs::read_to_string(&menu_path) {
Ok(c) => c,
Err(_) => return,
};
let re = Regex::new(r"\s*(.*?)\s*\[(.*)\]").unwrap();
let currentmenuitem_conf = self.conf("currentmenuitem");
let menuitem_conf = self.conf("menuitem");
let menucategory_conf = self.conf("menucategory");
for line in menu_content.lines() {
let line = line.trim();
if line.is_empty() || line.starts_with('#') {
continue;
}
if let Ok(Some(caps)) = re.captures(line) {
// Menu item
let mut link = caps.get(2).unwrap().as_str().to_string();
let menu_text = caps.get(1).unwrap().as_str();
let option = if link.starts_with('\\') {
link = link[1..].to_string();
" target=\"_blank\""
} else {
""
};
// Don't use prefix for absolute links
if !link.contains("://") {
link = format!("{}{}", prefix, allreplace(&link));
}
// Replace spaces with (except in {{ }} blocks)
let mut menuitem = String::new();
for group in menu_text.split("{{").flat_map(|s| {
let mut v: Vec<(&str, bool)> = Vec::new();
let parts: Vec<&str> = s.splitn(2, "}}").collect();
if parts.len() == 2 {
v.push((parts[0], true)); // inside {{ }}
v.push((parts[1], false));
} else {
v.push((parts[0], false));
}
v
}) {
if group.1 {
menuitem.push_str(group.0);
} else {
// Collapse one or more spaces into a single ~ (→ ),
// matching the Python original's re.sub(r' +', '~', ...).
let replaced = re_replace_all(r"(?<!\\n) +", group.0, "~");
let processed = self.br(&replaced, false);
menuitem.push_str(&processed);
}
}
if !current.is_empty() && link.ends_with(current) {
self.hb(¤tmenuitem_conf, &link, Some(&menuitem), Some(option));
} else {
self.hb(&menuitem_conf, &link, Some(&menuitem), Some(option));
}
} else {
// Menu category
let processed = self.br(line, false);
self.hb(&menucategory_conf, &processed, None, None);
}
}
}
// =========================================================================
// Main file processing
// =========================================================================
/// Process the jemdoc input file and generate HTML output.
pub fn proc_file(&mut self) {
self.linenum = 0;
let mut menu: Option<(String, String, String)> = None; // (menufile, current, prefix)
let mut show_footer = true;
let mut show_source_link = false;
let mut show_last_updated = true;
let mut show_last_updated_time = true;
let mut no_default_css = false;
let mut fwtitle = false;
let mut css: Vec<String> = Vec::new();
let mut js: Vec<String> = Vec::new();
let mut title: Option<String> = None;
// Parse header directives (lines starting with #)
while self.pc(false) == "#" {
let line = match self.nl(false, true) {
Some((l, _)) => l,
None => break,
};
let line = line.trim().to_string();
// Check for includes
if line.starts_with('#') && self.do_includes(line[1..].trim()) {
continue;
}
if let Some(directives) = line.strip_prefix("# jemdoc:") {
let re_braces = Regex::new(r"(?ms)(?<!\\)\{(.*?)(?<!\\)\}").unwrap();
for directive in directives.split(',') {
let d = directive.trim();
if d.starts_with("menu") {
let mut g: Vec<String> = Vec::new();
let mut ss = 0;
while let Ok(Some(caps)) = re_braces.captures_from_pos(d, ss) {
let m = caps.get(0).unwrap();
g.push(caps.get(1).unwrap().as_str().to_string());
ss = m.end();
}
if g.len() >= 2 {
let prefix = if g.len() >= 3 {
g[2].clone()
} else {
String::new()
};
menu = Some((g[0].clone(), g[1].clone(), prefix));
}
} else if d.starts_with("nofooter") {
show_footer = false;
} else if d.starts_with("nodate") {
show_last_updated = false;
} else if d.starts_with("notime") {
show_last_updated_time = false;
} else if d.starts_with("fwtitle") {
fwtitle = true;
} else if d.starts_with("showsource") {
show_source_link = true;
} else if d.starts_with("nodefaultcss") {
no_default_css = true;
} else if d.starts_with("addcss") {
let mut ss = 0;
while let Ok(Some(caps)) = re_braces.captures_from_pos(d, ss) {
let m = caps.get(0).unwrap();
css.push(caps.get(1).unwrap().as_str().to_string());
ss = m.end();
}
} else if d.starts_with("addjs") {
let mut ss = 0;
while let Ok(Some(caps)) = re_braces.captures_from_pos(d, ss) {
let m = caps.get(0).unwrap();
js.push(caps.get(1).unwrap().as_str().to_string());
ss = m.end();
}
} else if d.starts_with("analytics") {
let ss = 0;
if let Ok(Some(caps)) = re_braces.captures_from_pos(d, ss) {
self.analytics = Some(caps.get(1).unwrap().as_str().to_string());
}
} else if d.starts_with("title") {
let ss = 0;
if let Ok(Some(caps)) = re_braces.captures_from_pos(d, ss) {
title = Some(caps.get(1).unwrap().as_str().to_string());
}
} else if d.starts_with("noeqs") {
self.eqs = false;
}
}
}
}
// Output the first bit of HTML
let firstbit = self.conf("firstbit");
self.out(&firstbit);
if !no_default_css {
let defaultcss = self.conf("defaultcss");
self.out(&defaultcss);
}
// Add per-file CSS
let specificcss = self.conf("specificcss");
for c in &mut css {
if !c.contains(".css") {
c.push_str(".css");
}
self.hb(&specificcss, c, None, None);
}
// Add per-file JS
let specificjs = self.conf("specificjs");
for j in &js {
self.hb(&specificjs, j, None, None);
}
// Look for a title (line starting with =)
let t = if self.pc(true) == "=" {
let (title_line, _) = self.nl(false, false).unwrap_or_default();
let mut processed = self.br(&title_line, false);
// Remove trailing \n
if processed.ends_with('\n') {
processed.pop();
}
if title.is_none() {
title = Some(re_replace_all(r" *(<br />)|( ) *", &processed, " ").to_string());
}
Some(processed)
} else {
None
};
// Window title
let window_title = title.clone().unwrap_or_default();
let windowtitle_conf = self.conf("windowtitle");
self.hb(&windowtitle_conf, &window_title, None, None);
// MathJax (skip if equations are disabled via noeqs)
if self.eqs {
let mathjax = self.conf("mathjax");
self.out(&mathjax);
}
// Body start
let bodystart = self.conf("bodystart");
self.out(&bodystart);
// Analytics
if let Some(analytics_id) = self.analytics.clone() {
let analytics_conf = self.conf("analytics");
self.hb(&analytics_conf, &analytics_id, None, None);
}
// Full-width title
if fwtitle {
let fwtitlestart = self.conf("fwtitlestart");
self.out(&fwtitlestart);
self.insert_title(t.as_deref());
let fwtitleend = self.conf("fwtitleend");
self.out(&fwtitleend);
}
// Menu
if let Some((mname, current, prefix)) = &menu {
let menustart = self.conf("menustart");
self.out(&menustart);
self.insert_menu_items(mname, current, prefix);
let menuend = self.conf("menuend");
self.out(&menuend);
} else {
let nomenu = self.conf("nomenu");
self.out(&nomenu);
}
// Insert title (if not full-width)
if !fwtitle {
self.insert_title(t.as_deref());
}
// Main content processing loop
let mut infoblock = false;
let mut imgblock = false;
let mut tableblock = false;
loop {
let p = self.pc(true);
if p.is_empty() {
break;
}
if p == "\\(" {
// Whole-line equation
if !self.eqs {
// Skip the equation block without processing
let (s, _) = self.nl(false, false).unwrap_or_default();
if !s.trim().ends_with("\\)") {
while let Some((line, _)) = self.nl(false, true) {
if line.trim() == "\\)" {
break;
}
}
}
continue;
}
let (mut s, _) = self.nl(false, false).unwrap_or_default();
// Check if equation is single-line
if !s.trim().ends_with("\\)") {
while let Some((line, _)) = self.nl(false, true) {
s.push_str(&line);
if line.trim() == "\\)" {
break;
}
}
}
let r = self.br(s.trim(), false);
let r = mathjax_eq_resub(&r);
self.out(&r);
} else if p == "-" {
// Unordered list
self.dashlist(false);
} else if p == "." {
// Ordered list
self.dashlist(true);
} else if p == ":" {
// Definition list
self.colonlist();
} else if p == "=" {
// Heading
let (s, c) = self.nl(true, false).unwrap_or_default();
let s = s.trim_end_matches('\n');
let processed = self.br(s, false);
let heading = format!("<h{}>|</h{}>\n", c, c);
self.hb(&heading, &processed, None, None);
} else if p == "#" {
// Comment (already consumed by pc)
let _ = self.nl(false, false);
} else if p == "\n" {
// Blank line
let _ = self.nl(false, false);
} else if p == "~" {
// Block delimiter
let _ = self.nl(false, false);
if infoblock {
let infoblockend = self.conf("infoblockend");
self.out(&infoblockend);
infoblock = false;
let _ = self.nl(false, false);
continue;
} else if imgblock {
self.out("</td></tr></table>\n");
imgblock = false;
let _ = self.nl(false, false);
continue;
} else if tableblock {
self.out("</td></tr></table>\n");
tableblock = false;
let _ = self.nl(false, false);
continue;
} else {
// Start a new block
let mut g: Vec<String> = Vec::new();
if self.pc(true) == "{" {
let l = match self.nl(false, false) {
Some((line, _)) => allreplace(&line),
None => String::new(),
};
let re_braces = Regex::new(r"(?ms)(?<!\\)\{(.*?)(?<!\\)\}").unwrap();
let mut ss = 0;
while let Ok(Some(caps)) = re_braces.captures_from_pos(&l, ss) {
let m = caps.get(0).unwrap();
g.push(caps.get(1).unwrap().as_str().to_string());
ss = m.end();
}
}
// Process jemdoc markup in first group (title)
if !g.is_empty() {
let processed = self.br(&g[0], false);
g[0] = processed;
}
if g.is_empty() || g.len() == 1 {
// Info block
let infoblock_conf = self.conf("infoblock");
self.out(&infoblock_conf);
infoblock = true;
if g.len() == 1 {
let blocktitle_conf = self.conf("blocktitle");
self.hb(&blocktitle_conf, &g[0], None, None);
}
let infoblockcontent_conf = self.conf("infoblockcontent");
self.out(&infoblockcontent_conf);
} else if g.len() >= 2 && g[1] == "table" {
// Table block
let name = if g.len() >= 3 && !g[2].is_empty() {
format!(" id=\"{}\"", g[2])
} else {
String::new()
};
self.out(&format!(
"<table{}>\n<tr class=\"r1\"><td class=\"c1\">",
name
));
self.tablerow = 1;
self.tablecol = 1;
tableblock = true;
} else if g.len() == 2 {
// Code block
let title = if g[0].is_empty() {
None
} else {
Some(g[0].as_str())
};
let lang = g[1].clone();
self.codeblock(title, &lang);
} else if g.len() >= 4 && g[1] == "img_left" {
// Image block
let mut g = g;
while g.len() < 7 {
g.push(String::new());
}
if g[4].chars().all(|c| c.is_ascii_digit()) && !g[4].is_empty() {
g[4] = format!("{}px", g[4]);
}
if g[5].chars().all(|c| c.is_ascii_digit()) && !g[5].is_empty() {
g[5] = format!("{}px", g[5]);
}
self.out("<table class=\"imgtable\"><tr><td>\n");
if !g[6].is_empty() {
let (target, href) = if g[6].starts_with('/') {
("", g[6][1..].to_string())
} else {
(" target=\"_blank\"", g[6].clone())
};
self.out(&format!("<a href=\"{}\"{}>", href, target));
}
self.out(&format!("<img src=\"{}\"", g[2]));
self.out(&format!(" alt=\"{}\"", g[3]));
if !g[4].is_empty() {
self.out(&format!(" width=\"{}\"", g[4]));
}
if !g[5].is_empty() {
self.out(&format!(" height=\"{}\"", g[5]));
}
self.out(" />");
if !g[6].is_empty() {
self.out("</a>");
}
self.out(" </td>\n<td align=\"left\">");
imgblock = true;
}
}
} else {
// Regular paragraph
let (s, _) = match self.np(false, true) {
Some(v) => v,
None => break,
};
if !s.is_empty() {
let processed = self.br(&s, tableblock);
if tableblock {
self.hb("|\n", &processed, None, None);
} else {
self.hb("<p>|</p>\n", &processed, None, None);
}
}
}
}
// Footer
if show_footer {
let footerstart = self.conf("footerstart");
self.out(&footerstart);
if show_last_updated {
let s = {
let now = jiff::Zoned::now();
if show_last_updated_time {
now.strftime("%Y-%m-%d %H:%M:%S %Z").to_string()
} else {
now.strftime("%Y-%m-%d").to_string()
}
};
let lastupdated = self.conf("lastupdated");
self.hb(&lastupdated, &s, None, None);
} else {
let generatedby = self.conf("generatedby");
self.out(&generatedby);
}
if show_source_link {
let basename = std::path::Path::new(&self.inname)
.file_name()
.map(|f| f.to_string_lossy().to_string())
.unwrap_or_else(|| self.inname.clone());
let sourcelink = self.conf("sourcelink");
self.hb(&sourcelink, &basename, None, None);
}
let footerend = self.conf("footerend");
self.out(&footerend);
}
// Close menu/layout
if menu.is_some() {
let menulastbit = self.conf("menulastbit");
self.out(&menulastbit);
} else {
let nomenulastbit = self.conf("nomenulastbit");
self.out(&nomenulastbit);
}
// Body end
let bodyend = self.conf("bodyend");
self.out(&bodyend);
// Flush output
let _ = self.outf.flush();
}
}