20
20
//! edition: Edition::Edition2015,
21
21
//! playground: &None,
22
22
//! heading_offset: HeadingOffset::H2,
23
+ //! custom_code_classes_in_docs: true,
23
24
//! };
24
25
//! let html = md.into_string();
25
26
//! // ... something using html
@@ -95,6 +96,8 @@ pub struct Markdown<'a> {
95
96
/// Offset at which we render headings.
96
97
/// E.g. if `heading_offset: HeadingOffset::H2`, then `# something` renders an `<h2>`.
97
98
pub heading_offset : HeadingOffset ,
99
+ /// `true` if the `custom_code_classes_in_docs` feature is enabled.
100
+ pub custom_code_classes_in_docs : bool ,
98
101
}
99
102
/// A struct like `Markdown` that renders the markdown with a table of contents.
100
103
pub ( crate ) struct MarkdownWithToc < ' a > {
@@ -103,6 +106,8 @@ pub(crate) struct MarkdownWithToc<'a> {
103
106
pub ( crate ) error_codes : ErrorCodes ,
104
107
pub ( crate ) edition : Edition ,
105
108
pub ( crate ) playground : & ' a Option < Playground > ,
109
+ /// `true` if the `custom_code_classes_in_docs` feature is enabled.
110
+ pub ( crate ) custom_code_classes_in_docs : bool ,
106
111
}
107
112
/// A tuple struct like `Markdown` that renders the markdown escaping HTML tags
108
113
/// and includes no paragraph tags.
@@ -203,6 +208,7 @@ struct CodeBlocks<'p, 'a, I: Iterator<Item = Event<'a>>> {
203
208
// Information about the playground if a URL has been specified, containing an
204
209
// optional crate name and the URL.
205
210
playground : & ' p Option < Playground > ,
211
+ custom_code_classes_in_docs : bool ,
206
212
}
207
213
208
214
impl < ' p , ' a , I : Iterator < Item = Event < ' a > > > CodeBlocks < ' p , ' a , I > {
@@ -211,8 +217,15 @@ impl<'p, 'a, I: Iterator<Item = Event<'a>>> CodeBlocks<'p, 'a, I> {
211
217
error_codes : ErrorCodes ,
212
218
edition : Edition ,
213
219
playground : & ' p Option < Playground > ,
220
+ custom_code_classes_in_docs : bool ,
214
221
) -> Self {
215
- CodeBlocks { inner : iter, check_error_codes : error_codes, edition, playground }
222
+ CodeBlocks {
223
+ inner : iter,
224
+ check_error_codes : error_codes,
225
+ edition,
226
+ playground,
227
+ custom_code_classes_in_docs,
228
+ }
216
229
}
217
230
}
218
231
@@ -242,8 +255,12 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'_, 'a, I> {
242
255
243
256
let parse_result = match kind {
244
257
CodeBlockKind :: Fenced ( ref lang) => {
245
- let parse_result =
246
- LangString :: parse_without_check ( lang, self . check_error_codes , false ) ;
258
+ let parse_result = LangString :: parse_without_check (
259
+ lang,
260
+ self . check_error_codes ,
261
+ false ,
262
+ self . custom_code_classes_in_docs ,
263
+ ) ;
247
264
if !parse_result. rust {
248
265
let added_classes = parse_result. added_classes ;
249
266
let lang_string = if let Some ( lang) = parse_result. unknown . first ( ) {
@@ -725,8 +742,17 @@ pub(crate) fn find_testable_code<T: doctest::Tester>(
725
742
error_codes : ErrorCodes ,
726
743
enable_per_target_ignores : bool ,
727
744
extra_info : Option < & ExtraInfo < ' _ > > ,
745
+ custom_code_classes_in_docs : bool ,
728
746
) {
729
- find_codes ( doc, tests, error_codes, enable_per_target_ignores, extra_info, false )
747
+ find_codes (
748
+ doc,
749
+ tests,
750
+ error_codes,
751
+ enable_per_target_ignores,
752
+ extra_info,
753
+ false ,
754
+ custom_code_classes_in_docs,
755
+ )
730
756
}
731
757
732
758
pub ( crate ) fn find_codes < T : doctest:: Tester > (
@@ -736,6 +762,7 @@ pub(crate) fn find_codes<T: doctest::Tester>(
736
762
enable_per_target_ignores : bool ,
737
763
extra_info : Option < & ExtraInfo < ' _ > > ,
738
764
include_non_rust : bool ,
765
+ custom_code_classes_in_docs : bool ,
739
766
) {
740
767
let mut parser = Parser :: new ( doc) . into_offset_iter ( ) ;
741
768
let mut prev_offset = 0 ;
@@ -754,6 +781,7 @@ pub(crate) fn find_codes<T: doctest::Tester>(
754
781
error_codes,
755
782
enable_per_target_ignores,
756
783
extra_info,
784
+ custom_code_classes_in_docs,
757
785
)
758
786
}
759
787
}
@@ -1153,15 +1181,23 @@ impl LangString {
1153
1181
string : & str ,
1154
1182
allow_error_code_check : ErrorCodes ,
1155
1183
enable_per_target_ignores : bool ,
1184
+ custom_code_classes_in_docs : bool ,
1156
1185
) -> Self {
1157
- Self :: parse ( string, allow_error_code_check, enable_per_target_ignores, None )
1186
+ Self :: parse (
1187
+ string,
1188
+ allow_error_code_check,
1189
+ enable_per_target_ignores,
1190
+ None ,
1191
+ custom_code_classes_in_docs,
1192
+ )
1158
1193
}
1159
1194
1160
1195
fn parse (
1161
1196
string : & str ,
1162
1197
allow_error_code_check : ErrorCodes ,
1163
1198
enable_per_target_ignores : bool ,
1164
1199
extra : Option < & ExtraInfo < ' _ > > ,
1200
+ custom_code_classes_in_docs : bool ,
1165
1201
) -> Self {
1166
1202
let allow_error_code_check = allow_error_code_check. as_bool ( ) ;
1167
1203
let mut seen_rust_tags = false ;
@@ -1197,7 +1233,11 @@ impl LangString {
1197
1233
seen_rust_tags = true ;
1198
1234
}
1199
1235
LangStringToken :: LangToken ( "custom" ) => {
1200
- seen_custom_tag = true ;
1236
+ if custom_code_classes_in_docs {
1237
+ seen_custom_tag = true ;
1238
+ } else {
1239
+ seen_other_tags = true ;
1240
+ }
1201
1241
}
1202
1242
LangStringToken :: LangToken ( "test_harness" ) => {
1203
1243
data. test_harness = true ;
@@ -1268,11 +1308,16 @@ impl LangString {
1268
1308
data. unknown . push ( x. to_owned ( ) ) ;
1269
1309
}
1270
1310
LangStringToken :: KeyValueAttribute ( key, value) => {
1271
- if key == "class" {
1272
- data. added_classes . push ( value. to_owned ( ) ) ;
1273
- } else if let Some ( extra) = extra {
1274
- extra
1275
- . error_invalid_codeblock_attr ( format ! ( "unsupported attribute `{key}`" ) ) ;
1311
+ if custom_code_classes_in_docs {
1312
+ if key == "class" {
1313
+ data. added_classes . push ( value. to_owned ( ) ) ;
1314
+ } else if let Some ( extra) = extra {
1315
+ extra. error_invalid_codeblock_attr ( format ! (
1316
+ "unsupported attribute `{key}`"
1317
+ ) ) ;
1318
+ }
1319
+ } else {
1320
+ seen_other_tags = true ;
1276
1321
}
1277
1322
}
1278
1323
LangStringToken :: ClassAttribute ( class) => {
@@ -1302,6 +1347,7 @@ impl Markdown<'_> {
1302
1347
edition,
1303
1348
playground,
1304
1349
heading_offset,
1350
+ custom_code_classes_in_docs,
1305
1351
} = self ;
1306
1352
1307
1353
// This is actually common enough to special-case
@@ -1324,7 +1370,7 @@ impl Markdown<'_> {
1324
1370
let p = Footnotes :: new ( p) ;
1325
1371
let p = LinkReplacer :: new ( p. map ( |( ev, _) | ev) , links) ;
1326
1372
let p = TableWrapper :: new ( p) ;
1327
- let p = CodeBlocks :: new ( p, codes, edition, playground) ;
1373
+ let p = CodeBlocks :: new ( p, codes, edition, playground, custom_code_classes_in_docs ) ;
1328
1374
html:: push_html ( & mut s, p) ;
1329
1375
1330
1376
s
@@ -1333,7 +1379,14 @@ impl Markdown<'_> {
1333
1379
1334
1380
impl MarkdownWithToc < ' _ > {
1335
1381
pub ( crate ) fn into_string ( self ) -> String {
1336
- let MarkdownWithToc { content : md, ids, error_codes : codes, edition, playground } = self ;
1382
+ let MarkdownWithToc {
1383
+ content : md,
1384
+ ids,
1385
+ error_codes : codes,
1386
+ edition,
1387
+ playground,
1388
+ custom_code_classes_in_docs,
1389
+ } = self ;
1337
1390
1338
1391
let p = Parser :: new_ext ( md, main_body_opts ( ) ) . into_offset_iter ( ) ;
1339
1392
@@ -1345,7 +1398,7 @@ impl MarkdownWithToc<'_> {
1345
1398
let p = HeadingLinks :: new ( p, Some ( & mut toc) , ids, HeadingOffset :: H1 ) ;
1346
1399
let p = Footnotes :: new ( p) ;
1347
1400
let p = TableWrapper :: new ( p. map ( |( ev, _) | ev) ) ;
1348
- let p = CodeBlocks :: new ( p, codes, edition, playground) ;
1401
+ let p = CodeBlocks :: new ( p, codes, edition, playground, custom_code_classes_in_docs ) ;
1349
1402
html:: push_html ( & mut s, p) ;
1350
1403
}
1351
1404
@@ -1786,7 +1839,11 @@ pub(crate) struct RustCodeBlock {
1786
1839
1787
1840
/// Returns a range of bytes for each code block in the markdown that is tagged as `rust` or
1788
1841
/// untagged (and assumed to be rust).
1789
- pub ( crate ) fn rust_code_blocks ( md : & str , extra_info : & ExtraInfo < ' _ > ) -> Vec < RustCodeBlock > {
1842
+ pub ( crate ) fn rust_code_blocks (
1843
+ md : & str ,
1844
+ extra_info : & ExtraInfo < ' _ > ,
1845
+ custom_code_classes_in_docs : bool ,
1846
+ ) -> Vec < RustCodeBlock > {
1790
1847
let mut code_blocks = vec ! [ ] ;
1791
1848
1792
1849
if md. is_empty ( ) {
@@ -1803,7 +1860,13 @@ pub(crate) fn rust_code_blocks(md: &str, extra_info: &ExtraInfo<'_>) -> Vec<Rust
1803
1860
let lang_string = if syntax. is_empty ( ) {
1804
1861
Default :: default ( )
1805
1862
} else {
1806
- LangString :: parse ( & * syntax, ErrorCodes :: Yes , false , Some ( extra_info) )
1863
+ LangString :: parse (
1864
+ & * syntax,
1865
+ ErrorCodes :: Yes ,
1866
+ false ,
1867
+ Some ( extra_info) ,
1868
+ custom_code_classes_in_docs,
1869
+ )
1807
1870
} ;
1808
1871
if !lang_string. rust {
1809
1872
continue ;
0 commit comments