@@ -41,7 +41,9 @@ fn extract_leading_metadata<'a>(s: &'a str) -> (Vec<&'a str>, &'a str) {
41
41
/// Render `input` (e.g. "foo.md") into an HTML file in `output`
42
42
/// (e.g. output = "bar" => "bar/foo.html").
43
43
pub fn render ( input : & str , mut output : Path , matches : & getopts:: Matches ,
44
- external_html : & ExternalHtml , include_toc : bool ) -> int {
44
+ external_html : & ExternalHtml ,
45
+ include_toc : bool ,
46
+ enable_math : bool ) -> int {
45
47
let input_p = Path :: new ( input) ;
46
48
output. push ( input_p. filestem ( ) . unwrap ( ) ) ;
47
49
output. set_extension ( "html" ) ;
@@ -79,12 +81,18 @@ pub fn render(input: &str, mut output: Path, matches: &getopts::Matches,
79
81
80
82
reset_headers ( ) ;
81
83
84
+ if enable_math {
85
+ markdown:: enable_math. replace ( Some ( true ) ) ;
86
+ }
87
+
82
88
let rendered = if include_toc {
83
89
format ! ( "{}" , MarkdownWithToc ( text) )
84
90
} else {
85
91
format ! ( "{}" , Markdown ( text) )
86
92
} ;
87
93
94
+ let saw_math = enable_math && markdown:: math_seen. get ( ) . map_or ( false , |x| * x) ;
95
+
88
96
let err = write ! (
89
97
& mut out,
90
98
r#"<!DOCTYPE html>
@@ -95,6 +103,7 @@ pub fn render(input: &str, mut output: Path, matches: &getopts::Matches,
95
103
<title>{title}</title>
96
104
97
105
{css}
106
+ {math_css}
98
107
{in_header}
99
108
</head>
100
109
<body class="rustdoc">
@@ -112,6 +121,7 @@ pub fn render(input: &str, mut output: Path, matches: &getopts::Matches,
112
121
window.playgroundUrl = "{playground}";
113
122
</script>
114
123
{after_content}
124
+ {math_js}
115
125
</body>
116
126
</html>"# ,
117
127
title = Escape ( title) ,
@@ -121,6 +131,26 @@ pub fn render(input: &str, mut output: Path, matches: &getopts::Matches,
121
131
text = rendered,
122
132
after_content = external_html. after_content,
123
133
playground = playground,
134
+
135
+ math_css = if saw_math {
136
+ "<link rel=\" stylesheet\" type=\" text/css\" href=\" katex/katex.min.css\" >
137
+ <style>.math-display { text-align: center; }</style>"
138
+ } else {
139
+ ""
140
+ } ,
141
+ math_js = if saw_math {
142
+ r#"<script src="katex/katex.min.js"></script>
143
+ <script type="text/javascript">
144
+ var elements = document.getElementsByClassName("latex-math");
145
+ for (var i = 0; i < elements.length; i++) {
146
+ var e = elements[i];
147
+ var style = e.tagName == "P" ? "display" : "text";
148
+ katex.render("\\" + style + "style " + e.textContent, e);
149
+ }
150
+ </script>"#
151
+ } else {
152
+ ""
153
+ }
124
154
) ;
125
155
126
156
match err {
0 commit comments