Fossil SCM
Enhance the markdown formatter to ignore certain HTML tags such as <style>.
Commit
a3ab0c6186b43236eb1143d3921d9e0f747fe3a7707163ba35dbfde83295e5d3
Parent
4b98322350c39ff…
1 file changed
+33
-29
+33
-29
| --- src/markdown.c | ||
| +++ src/markdown.c | ||
| @@ -163,48 +163,45 @@ | ||
| 163 | 163 | |
| 164 | 164 | /* html_tag -- structure for quick HTML tag search (inspired from discount) */ |
| 165 | 165 | struct html_tag { |
| 166 | 166 | const char *text; |
| 167 | 167 | int size; |
| 168 | + int flags; | |
| 168 | 169 | }; |
| 169 | 170 | |
| 171 | +/* Allowed bits in html_tag.flags */ | |
| 172 | +#define HTMLTAG_FORBIDDEN 0x001 /* escape */ | |
| 170 | 173 | |
| 171 | 174 | |
| 172 | 175 | /******************** |
| 173 | 176 | * GLOBAL VARIABLES * |
| 174 | 177 | ********************/ |
| 175 | 178 | |
| 176 | 179 | /* block_tags -- recognised block tags, sorted by cmp_html_tag */ |
| 177 | 180 | static const struct html_tag block_tags[] = { |
| 178 | - { "p", 1 }, | |
| 179 | - { "dl", 2 }, | |
| 180 | - { "h1", 2 }, | |
| 181 | - { "h2", 2 }, | |
| 182 | - { "h3", 2 }, | |
| 183 | - { "h4", 2 }, | |
| 184 | - { "h5", 2 }, | |
| 185 | - { "h6", 2 }, | |
| 186 | - { "ol", 2 }, | |
| 187 | - { "ul", 2 }, | |
| 188 | - { "del", 3 }, | |
| 189 | - { "div", 3 }, | |
| 190 | - { "ins", 3 }, | |
| 191 | - { "pre", 3 }, | |
| 192 | - { "form", 4 }, | |
| 193 | - { "math", 4 }, | |
| 194 | - { "table", 5 }, | |
| 195 | - { "iframe", 6 }, | |
| 196 | - { "script", 6 }, | |
| 197 | - { "fieldset", 8 }, | |
| 198 | - { "noscript", 8 }, | |
| 199 | - { "blockquote", 10 } | |
| 181 | + { "p", 1, 0 }, | |
| 182 | + { "dl", 2, 0 }, | |
| 183 | + { "h1", 2, 0 }, | |
| 184 | + { "h2", 2, 0 }, | |
| 185 | + { "h3", 2, 0 }, | |
| 186 | + { "h4", 2, 0 }, | |
| 187 | + { "h5", 2, 0 }, | |
| 188 | + { "h6", 2, 0 }, | |
| 189 | + { "ol", 2, 0 }, | |
| 190 | + { "ul", 2, 0 }, | |
| 191 | + { "div", 3, 0 }, | |
| 192 | + { "pre", 3, 0 }, | |
| 193 | + { "form", 4, HTMLTAG_FORBIDDEN }, | |
| 194 | + { "math", 4, 0 }, | |
| 195 | + { "style", 5, HTMLTAG_FORBIDDEN }, | |
| 196 | + { "table", 5, 0 }, | |
| 197 | + { "iframe", 6, HTMLTAG_FORBIDDEN }, | |
| 198 | + { "script", 6, HTMLTAG_FORBIDDEN }, | |
| 199 | + { "fieldset", 8, 0 }, | |
| 200 | + { "noscript", 8, HTMLTAG_FORBIDDEN }, | |
| 201 | + { "blockquote", 10, 0 } | |
| 200 | 202 | }; |
| 201 | - | |
| 202 | -#define INS_TAG (block_tags + 12) | |
| 203 | -#define DEL_TAG (block_tags + 10) | |
| 204 | - | |
| 205 | - | |
| 206 | 203 | |
| 207 | 204 | /*************************** |
| 208 | 205 | * STATIC HELPER FUNCTIONS * |
| 209 | 206 | ***************************/ |
| 210 | 207 | |
| @@ -1783,12 +1780,11 @@ | ||
| 1783 | 1780 | } |
| 1784 | 1781 | } |
| 1785 | 1782 | #endif |
| 1786 | 1783 | |
| 1787 | 1784 | /* if not found, trying a second pass looking for indented match */ |
| 1788 | - /* but not if tag is "ins" or "del" (following original Markdown.pl) */ | |
| 1789 | - if( !found && curtag!=INS_TAG && curtag!=DEL_TAG ){ | |
| 1785 | + if( !found ){ | |
| 1790 | 1786 | i = 1; |
| 1791 | 1787 | while( i<size ){ |
| 1792 | 1788 | i++; |
| 1793 | 1789 | while( i<size && !(data[i-1]=='<' && data[i]=='/') ){ i++; } |
| 1794 | 1790 | if( (i+2+curtag->size)>=size ) break; |
| @@ -1798,10 +1794,18 @@ | ||
| 1798 | 1794 | found = 1; |
| 1799 | 1795 | break; |
| 1800 | 1796 | } |
| 1801 | 1797 | } |
| 1802 | 1798 | } |
| 1799 | + | |
| 1800 | + /* Do not display certain HTML tags */ | |
| 1801 | + if( curtag->flags & HTMLTAG_FORBIDDEN ){ | |
| 1802 | + if( !found ){ | |
| 1803 | + for(i=1; i<size && data[i]!='>'; i++){} | |
| 1804 | + } | |
| 1805 | + return i; | |
| 1806 | + } | |
| 1803 | 1807 | |
| 1804 | 1808 | if( !found ) return 0; |
| 1805 | 1809 | |
| 1806 | 1810 | /* the end of the block has been found */ |
| 1807 | 1811 | blob_init(&work, data, i); |
| 1808 | 1812 |
| --- src/markdown.c | |
| +++ src/markdown.c | |
| @@ -163,48 +163,45 @@ | |
| 163 | |
| 164 | /* html_tag -- structure for quick HTML tag search (inspired from discount) */ |
| 165 | struct html_tag { |
| 166 | const char *text; |
| 167 | int size; |
| 168 | }; |
| 169 | |
| 170 | |
| 171 | |
| 172 | /******************** |
| 173 | * GLOBAL VARIABLES * |
| 174 | ********************/ |
| 175 | |
| 176 | /* block_tags -- recognised block tags, sorted by cmp_html_tag */ |
| 177 | static const struct html_tag block_tags[] = { |
| 178 | { "p", 1 }, |
| 179 | { "dl", 2 }, |
| 180 | { "h1", 2 }, |
| 181 | { "h2", 2 }, |
| 182 | { "h3", 2 }, |
| 183 | { "h4", 2 }, |
| 184 | { "h5", 2 }, |
| 185 | { "h6", 2 }, |
| 186 | { "ol", 2 }, |
| 187 | { "ul", 2 }, |
| 188 | { "del", 3 }, |
| 189 | { "div", 3 }, |
| 190 | { "ins", 3 }, |
| 191 | { "pre", 3 }, |
| 192 | { "form", 4 }, |
| 193 | { "math", 4 }, |
| 194 | { "table", 5 }, |
| 195 | { "iframe", 6 }, |
| 196 | { "script", 6 }, |
| 197 | { "fieldset", 8 }, |
| 198 | { "noscript", 8 }, |
| 199 | { "blockquote", 10 } |
| 200 | }; |
| 201 | |
| 202 | #define INS_TAG (block_tags + 12) |
| 203 | #define DEL_TAG (block_tags + 10) |
| 204 | |
| 205 | |
| 206 | |
| 207 | /*************************** |
| 208 | * STATIC HELPER FUNCTIONS * |
| 209 | ***************************/ |
| 210 | |
| @@ -1783,12 +1780,11 @@ | |
| 1783 | } |
| 1784 | } |
| 1785 | #endif |
| 1786 | |
| 1787 | /* if not found, trying a second pass looking for indented match */ |
| 1788 | /* but not if tag is "ins" or "del" (following original Markdown.pl) */ |
| 1789 | if( !found && curtag!=INS_TAG && curtag!=DEL_TAG ){ |
| 1790 | i = 1; |
| 1791 | while( i<size ){ |
| 1792 | i++; |
| 1793 | while( i<size && !(data[i-1]=='<' && data[i]=='/') ){ i++; } |
| 1794 | if( (i+2+curtag->size)>=size ) break; |
| @@ -1798,10 +1794,18 @@ | |
| 1798 | found = 1; |
| 1799 | break; |
| 1800 | } |
| 1801 | } |
| 1802 | } |
| 1803 | |
| 1804 | if( !found ) return 0; |
| 1805 | |
| 1806 | /* the end of the block has been found */ |
| 1807 | blob_init(&work, data, i); |
| 1808 |
| --- src/markdown.c | |
| +++ src/markdown.c | |
| @@ -163,48 +163,45 @@ | |
| 163 | |
| 164 | /* html_tag -- structure for quick HTML tag search (inspired from discount) */ |
| 165 | struct html_tag { |
| 166 | const char *text; |
| 167 | int size; |
| 168 | int flags; |
| 169 | }; |
| 170 | |
| 171 | /* Allowed bits in html_tag.flags */ |
| 172 | #define HTMLTAG_FORBIDDEN 0x001 /* escape */ |
| 173 | |
| 174 | |
| 175 | /******************** |
| 176 | * GLOBAL VARIABLES * |
| 177 | ********************/ |
| 178 | |
| 179 | /* block_tags -- recognised block tags, sorted by cmp_html_tag */ |
| 180 | static const struct html_tag block_tags[] = { |
| 181 | { "p", 1, 0 }, |
| 182 | { "dl", 2, 0 }, |
| 183 | { "h1", 2, 0 }, |
| 184 | { "h2", 2, 0 }, |
| 185 | { "h3", 2, 0 }, |
| 186 | { "h4", 2, 0 }, |
| 187 | { "h5", 2, 0 }, |
| 188 | { "h6", 2, 0 }, |
| 189 | { "ol", 2, 0 }, |
| 190 | { "ul", 2, 0 }, |
| 191 | { "div", 3, 0 }, |
| 192 | { "pre", 3, 0 }, |
| 193 | { "form", 4, HTMLTAG_FORBIDDEN }, |
| 194 | { "math", 4, 0 }, |
| 195 | { "style", 5, HTMLTAG_FORBIDDEN }, |
| 196 | { "table", 5, 0 }, |
| 197 | { "iframe", 6, HTMLTAG_FORBIDDEN }, |
| 198 | { "script", 6, HTMLTAG_FORBIDDEN }, |
| 199 | { "fieldset", 8, 0 }, |
| 200 | { "noscript", 8, HTMLTAG_FORBIDDEN }, |
| 201 | { "blockquote", 10, 0 } |
| 202 | }; |
| 203 | |
| 204 | /*************************** |
| 205 | * STATIC HELPER FUNCTIONS * |
| 206 | ***************************/ |
| 207 | |
| @@ -1783,12 +1780,11 @@ | |
| 1780 | } |
| 1781 | } |
| 1782 | #endif |
| 1783 | |
| 1784 | /* if not found, trying a second pass looking for indented match */ |
| 1785 | if( !found ){ |
| 1786 | i = 1; |
| 1787 | while( i<size ){ |
| 1788 | i++; |
| 1789 | while( i<size && !(data[i-1]=='<' && data[i]=='/') ){ i++; } |
| 1790 | if( (i+2+curtag->size)>=size ) break; |
| @@ -1798,10 +1794,18 @@ | |
| 1794 | found = 1; |
| 1795 | break; |
| 1796 | } |
| 1797 | } |
| 1798 | } |
| 1799 | |
| 1800 | /* Do not display certain HTML tags */ |
| 1801 | if( curtag->flags & HTMLTAG_FORBIDDEN ){ |
| 1802 | if( !found ){ |
| 1803 | for(i=1; i<size && data[i]!='>'; i++){} |
| 1804 | } |
| 1805 | return i; |
| 1806 | } |
| 1807 | |
| 1808 | if( !found ) return 0; |
| 1809 | |
| 1810 | /* the end of the block has been found */ |
| 1811 | blob_init(&work, data, i); |
| 1812 |