@@ -49,6 +49,8 @@ pub struct Markdown<'a>(pub &'a str);
4949/// A unit struct like `Markdown`, that renders the markdown with a
5050/// table of contents.
5151pub struct MarkdownWithToc < ' a > ( pub & ' a str ) ;
52+ /// A unit struct like `Markdown`, that renders the markdown escaping HTML tags.
53+ pub struct MarkdownHtml < ' a > ( pub & ' a str ) ;
5254
5355const DEF_OUNIT : libc:: size_t = 64 ;
5456const HOEDOWN_EXT_NO_INTRA_EMPHASIS : libc:: c_uint = 1 << 11 ;
@@ -58,6 +60,7 @@ const HOEDOWN_EXT_AUTOLINK: libc::c_uint = 1 << 3;
5860const HOEDOWN_EXT_STRIKETHROUGH : libc:: c_uint = 1 << 4 ;
5961const HOEDOWN_EXT_SUPERSCRIPT : libc:: c_uint = 1 << 8 ;
6062const HOEDOWN_EXT_FOOTNOTES : libc:: c_uint = 1 << 2 ;
63+ const HOEDOWN_HTML_ESCAPE : libc:: c_uint = 1 << 1 ;
6164
6265const HOEDOWN_EXTENSIONS : libc:: c_uint =
6366 HOEDOWN_EXT_NO_INTRA_EMPHASIS | HOEDOWN_EXT_TABLES |
@@ -220,7 +223,11 @@ thread_local!(pub static PLAYGROUND: RefCell<Option<(Option<String>, String)>> =
220223 RefCell :: new( None )
221224} ) ;
222225
223- pub fn render ( w : & mut fmt:: Formatter , s : & str , print_toc : bool ) -> fmt:: Result {
226+
227+ pub fn render ( w : & mut fmt:: Formatter ,
228+ s : & str ,
229+ print_toc : bool ,
230+ html_flags : libc:: c_uint ) -> fmt:: Result {
224231 extern fn block ( ob : * mut hoedown_buffer , orig_text : * const hoedown_buffer ,
225232 lang : * const hoedown_buffer , data : * const hoedown_renderer_data ) {
226233 unsafe {
@@ -383,7 +390,7 @@ pub fn render(w: &mut fmt::Formatter, s: &str, print_toc: bool) -> fmt::Result {
383390
384391 unsafe {
385392 let ob = hoedown_buffer_new ( DEF_OUNIT ) ;
386- let renderer = hoedown_html_renderer_new ( 0 , 0 ) ;
393+ let renderer = hoedown_html_renderer_new ( html_flags , 0 ) ;
387394 let mut opaque = MyOpaque {
388395 dfltblk : ( * renderer) . blockcode . unwrap ( ) ,
389396 toc_builder : if print_toc { Some ( TocBuilder :: new ( ) ) } else { None }
@@ -553,14 +560,23 @@ impl<'a> fmt::Display for Markdown<'a> {
553560 let Markdown ( md) = * self ;
554561 // This is actually common enough to special-case
555562 if md. is_empty ( ) { return Ok ( ( ) ) }
556- render ( fmt, md, false )
563+ render ( fmt, md, false , 0 )
557564 }
558565}
559566
560567impl < ' a > fmt:: Display for MarkdownWithToc < ' a > {
561568 fn fmt ( & self , fmt : & mut fmt:: Formatter ) -> fmt:: Result {
562569 let MarkdownWithToc ( md) = * self ;
563- render ( fmt, md, true )
570+ render ( fmt, md, true , 0 )
571+ }
572+ }
573+
574+ impl < ' a > fmt:: Display for MarkdownHtml < ' a > {
575+ fn fmt ( & self , fmt : & mut fmt:: Formatter ) -> fmt:: Result {
576+ let MarkdownHtml ( md) = * self ;
577+ // This is actually common enough to special-case
578+ if md. is_empty ( ) { return Ok ( ( ) ) }
579+ render ( fmt, md, false , HOEDOWN_HTML_ESCAPE )
564580 }
565581}
566582
@@ -613,7 +629,7 @@ pub fn plain_summary_line(md: &str) -> String {
613629
614630#[ cfg( test) ]
615631mod tests {
616- use super :: { LangString , Markdown } ;
632+ use super :: { LangString , Markdown , MarkdownHtml } ;
617633 use super :: plain_summary_line;
618634 use html:: render:: reset_ids;
619635
@@ -719,4 +735,15 @@ mod tests {
719735 t ( "# top header" , "top header" ) ;
720736 t ( "## header" , "header" ) ;
721737 }
738+
739+ #[ test]
740+ fn test_markdown_html_escape ( ) {
741+ fn t ( input : & str , expect : & str ) {
742+ let output = format ! ( "{}" , MarkdownHtml ( input) ) ;
743+ assert_eq ! ( output, expect) ;
744+ }
745+
746+ t ( "`Struct<'a, T>`" , "<p><code>Struct<'a, T></code></p>\n " ) ;
747+ t ( "Struct<'a, T>" , "<p>Struct<'a, T></p>\n " ) ;
748+ }
722749}
0 commit comments