Fossil SCM

fossil-scm / src / wikiformat.c
Source Blame History 3188 lines
dbda8d6… drh 1 /*
c19f34c… drh 2 ** Copyright (c) 2007 D. Richard Hipp
dbda8d6… drh 3 **
dbda8d6… drh 4 ** This program is free software; you can redistribute it and/or
c06edd2… drh 5 ** modify it under the terms of the Simplified BSD License (also
c06edd2… drh 6 ** known as the "2-Clause License" or "FreeBSD License".)
c06edd2… drh 7
dbda8d6… drh 8 ** This program is distributed in the hope that it will be useful,
c06edd2… drh 9 ** but without any warranty; without even the implied warranty of
c06edd2… drh 10 ** merchantability or fitness for a particular purpose.
dbda8d6… drh 11 **
dbda8d6… drh 12 ** Author contact information:
dbda8d6… drh 13 ** [email protected]
dbda8d6… drh 14 ** http://www.hwaci.com/drh/
dbda8d6… drh 15 **
dbda8d6… drh 16 *******************************************************************************
dbda8d6… drh 17 **
dbda8d6… drh 18 ** This file contains code to do formatting of wiki text.
dbda8d6… drh 19 */
dbda8d6… drh 20 #include "config.h"
c30cd93… jan.nijtmans 21 #include <assert.h>
dbda8d6… drh 22 #include "wikiformat.h"
dbda8d6… drh 23
dbda8d6… drh 24 #if INTERFACE
dbda8d6… drh 25 /*
dbda8d6… drh 26 ** Allowed wiki transformation operations
dbda8d6… drh 27 */
53b080b… drh 28 #define WIKI_HTMLONLY 0x0001 /* HTML markup only. No wiki */
53b080b… drh 29 #define WIKI_INLINE 0x0002 /* Do not surround with <p>..</p> */
e2bdc10… danield 30 /* available for reuse: 0x0004 --- formerly WIKI_NOBLOCK */
53b080b… drh 31 #define WIKI_BUTTONS 0x0008 /* Allow sub-menu buttons */
53b080b… drh 32 #define WIKI_NOBADLINKS 0x0010 /* Ignore broken hyperlinks */
53b080b… drh 33 #define WIKI_LINKSONLY 0x0020 /* No markup. Only decorate links */
53b080b… drh 34 #define WIKI_NEWLINE 0x0040 /* Honor \n - break lines at each \n */
53b080b… drh 35 #define WIKI_MARKDOWNLINKS 0x0080 /* Resolve hyperlinks as in markdown */
53b080b… drh 36 #define WIKI_SAFE 0x0100 /* Make the result safe for embedding */
53b080b… drh 37 #define WIKI_TARGET_BLANK 0x0200 /* Hyperlinks go to a new window */
53b080b… drh 38 #define WIKI_NOBRACKET 0x0400 /* Omit extra [..] around hyperlinks */
53b080b… drh 39 #define WIKI_ADMIN 0x0800 /* Ignore g.perm.Hyperlink */
f0d9fe2… drh 40 #define WIKI_MARK 0x1000 /* Add <mark>..</mark> around problems */
53b080b… drh 41
53b080b… drh 42 /*
53b080b… drh 43 ** Return values from wiki_convert
53b080b… drh 44 */
53b080b… drh 45 #define RENDER_LINK 0x0001 /* One or more hyperlinks rendered */
53b080b… drh 46 #define RENDER_ENTITY 0x0002 /* One or more HTML entities (ex: &lt;) */
53b080b… drh 47 #define RENDER_TAG 0x0004 /* One or more HTML tags */
53b080b… drh 48 #define RENDER_BLOCKTAG 0x0008 /* One or more HTML block tags (ex: <p>) */
53b080b… drh 49 #define RENDER_BLOCK 0x0010 /* Block wiki (paragraphs, etc.) */
f0d9fe2… drh 50 #define RENDER_MARK 0x0020 /* Output contains <mark>..</mark> */
53b080b… drh 51 #define RENDER_BADLINK 0x0100 /* Bad hyperlink syntax seen */
53b080b… drh 52 #define RENDER_BADTARGET 0x0200 /* Bad hyperlink target */
53b080b… drh 53 #define RENDER_BADTAG 0x0400 /* Bad HTML tag or tag syntax */
53b080b… drh 54 #define RENDER_BADENTITY 0x0800 /* Bad HTML entity syntax */
53b080b… drh 55 #define RENDER_BADHTML 0x1000 /* Bad HTML seen */
53b080b… drh 56 #define RENDER_ERROR 0x8000 /* Some other kind of error */
53b080b… drh 57 /* Composite values: */
53b080b… drh 58 #define RENDER_ANYERROR 0x9f00 /* Mask for any kind of error */
53b080b… drh 59
53b080b… drh 60 #endif /* INTERFACE */
dbda8d6… drh 61
dbda8d6… drh 62
dbda8d6… drh 63 /*
dbda8d6… drh 64 ** These are the only markup attributes allowed.
dbda8d6… drh 65 */
0652717… stephan 66 enum allowed_attr_t {
0652717… stephan 67 ATTR_ALIGN = 1,
0652717… stephan 68 ATTR_ALT,
0652717… stephan 69 ATTR_BGCOLOR,
0652717… stephan 70 ATTR_BORDER,
0652717… stephan 71 ATTR_CELLPADDING,
0652717… stephan 72 ATTR_CELLSPACING,
6436ecf… wyoung 73 ATTR_CITE,
0652717… stephan 74 ATTR_CLASS,
0652717… stephan 75 ATTR_CLEAR,
0652717… stephan 76 ATTR_COLOR,
0652717… stephan 77 ATTR_COLSPAN,
0652717… stephan 78 ATTR_COMPACT,
6436ecf… wyoung 79 ATTR_DATETIME,
0652717… stephan 80 ATTR_FACE,
6436ecf… wyoung 81 ATTR_FOR,
0652717… stephan 82 ATTR_HEIGHT,
6436ecf… wyoung 83 ATTR_HIGH,
0652717… stephan 84 ATTR_HREF,
0652717… stephan 85 ATTR_HSPACE,
0652717… stephan 86 ATTR_ID,
0652717… stephan 87 ATTR_LINKS,
6436ecf… wyoung 88 ATTR_LOW,
6436ecf… wyoung 89 ATTR_MAX,
6436ecf… wyoung 90 ATTR_MEDIA,
6436ecf… wyoung 91 ATTR_MIN,
0652717… stephan 92 ATTR_NAME,
6436ecf… wyoung 93 ATTR_OPEN,
6436ecf… wyoung 94 ATTR_OPTIMUM,
0652717… stephan 95 ATTR_ROWSPAN,
0652717… stephan 96 ATTR_SIZE,
6436ecf… wyoung 97 ATTR_SIZES,
0652717… stephan 98 ATTR_SRC,
6436ecf… wyoung 99 ATTR_SRCSET,
0652717… stephan 100 ATTR_START,
0652717… stephan 101 ATTR_STYLE,
0652717… stephan 102 ATTR_TARGET,
b8078ae… drh 103 ATTR_TITLE,
0652717… stephan 104 ATTR_TYPE,
0652717… stephan 105 ATTR_VALIGN,
0652717… stephan 106 ATTR_VALUE,
0652717… stephan 107 ATTR_VSPACE,
0652717… stephan 108 ATTR_WIDTH
0652717… stephan 109 };
0652717… stephan 110
6436ecf… wyoung 111 typedef uint64_t amsk_t;
6436ecf… wyoung 112 #define AMSK_ALIGN ((amsk_t)1 << 0)
6436ecf… wyoung 113 #define AMSK_ALT ((amsk_t)1 << 1)
6436ecf… wyoung 114 #define AMSK_BGCOLOR ((amsk_t)1 << 2)
6436ecf… wyoung 115 #define AMSK_BORDER ((amsk_t)1 << 3)
6436ecf… wyoung 116 #define AMSK_CELLPADDING ((amsk_t)1 << 4)
6436ecf… wyoung 117 #define AMSK_CELLSPACING ((amsk_t)1 << 5)
6436ecf… wyoung 118 #define AMSK_CLASS ((amsk_t)1 << 6)
6436ecf… wyoung 119 #define AMSK_CLEAR ((amsk_t)1 << 7)
6436ecf… wyoung 120 #define AMSK_COLOR ((amsk_t)1 << 8)
6436ecf… wyoung 121 #define AMSK_COLSPAN ((amsk_t)1 << 9)
6436ecf… wyoung 122 #define AMSK_COMPACT ((amsk_t)1 << 10)
6436ecf… wyoung 123 #define AMSK_FACE ((amsk_t)1 << 11)
6436ecf… wyoung 124 #define AMSK_HEIGHT ((amsk_t)1 << 12)
6436ecf… wyoung 125 #define AMSK_HREF ((amsk_t)1 << 13)
6436ecf… wyoung 126 #define AMSK_HSPACE ((amsk_t)1 << 14)
6436ecf… wyoung 127 #define AMSK_ID ((amsk_t)1 << 15)
6436ecf… wyoung 128 #define AMSK_LINKS ((amsk_t)1 << 16)
6436ecf… wyoung 129 #define AMSK_NAME ((amsk_t)1 << 17)
6436ecf… wyoung 130 #define AMSK_OPEN ((amsk_t)1 << 18)
6436ecf… wyoung 131 #define AMSK_ROWSPAN ((amsk_t)1 << 19)
6436ecf… wyoung 132 #define AMSK_SIZE ((amsk_t)1 << 20)
6436ecf… wyoung 133 #define AMSK_SRC ((amsk_t)1 << 21)
6436ecf… wyoung 134 #define AMSK_START ((amsk_t)1 << 22)
6436ecf… wyoung 135 #define AMSK_STYLE ((amsk_t)1 << 23)
6436ecf… wyoung 136 #define AMSK_TARGET ((amsk_t)1 << 24)
6436ecf… wyoung 137 #define AMSK_TITLE ((amsk_t)1 << 25)
6436ecf… wyoung 138 #define AMSK_TYPE ((amsk_t)1 << 26)
6436ecf… wyoung 139 #define AMSK_VALIGN ((amsk_t)1 << 27)
6436ecf… wyoung 140 #define AMSK_VALUE ((amsk_t)1 << 28)
6436ecf… wyoung 141 #define AMSK_VSPACE ((amsk_t)1 << 29)
6436ecf… wyoung 142 #define AMSK_WIDTH ((amsk_t)1 << 30)
6436ecf… wyoung 143 #define AMSK_CITE ((amsk_t)1 << 31)
6436ecf… wyoung 144 #define AMSK_DATETIME ((amsk_t)1 << 32)
dbda8d6… drh 145
dbda8d6… drh 146 static const struct AllowedAttribute {
dbda8d6… drh 147 const char *zName;
6436ecf… wyoung 148 amsk_t iMask;
dbda8d6… drh 149 } aAttribute[] = {
0652717… stephan 150 /* These indexes MUST line up with their
0652717… stephan 151 corresponding allowed_attr_t enum values.
0652717… stephan 152 */
d0305b3… aku 153 { 0, 0 },
0652717… stephan 154 { "align", AMSK_ALIGN },
0652717… stephan 155 { "alt", AMSK_ALT },
0652717… stephan 156 { "bgcolor", AMSK_BGCOLOR },
0652717… stephan 157 { "border", AMSK_BORDER },
0652717… stephan 158 { "cellpadding", AMSK_CELLPADDING },
0652717… stephan 159 { "cellspacing", AMSK_CELLSPACING },
6436ecf… wyoung 160 { "cite", AMSK_CITE },
0652717… stephan 161 { "class", AMSK_CLASS },
0652717… stephan 162 { "clear", AMSK_CLEAR },
0652717… stephan 163 { "color", AMSK_COLOR },
0652717… stephan 164 { "colspan", AMSK_COLSPAN },
0652717… stephan 165 { "compact", AMSK_COMPACT },
6436ecf… wyoung 166 { "datetime", AMSK_DATETIME },
0652717… stephan 167 { "face", AMSK_FACE },
6436ecf… wyoung 168 { "for", 0 },
0652717… stephan 169 { "height", AMSK_HEIGHT },
6436ecf… wyoung 170 { "high", 0 },
0652717… stephan 171 { "href", AMSK_HREF },
0652717… stephan 172 { "hspace", AMSK_HSPACE },
0652717… stephan 173 { "id", AMSK_ID },
0652717… stephan 174 { "links", AMSK_LINKS },
6436ecf… wyoung 175 { "low", 0 },
6436ecf… wyoung 176 { "max", 0 },
6436ecf… wyoung 177 { "media", 0 },
6436ecf… wyoung 178 { "min", 0 },
0652717… stephan 179 { "name", AMSK_NAME },
6436ecf… wyoung 180 { "open", AMSK_OPEN },
6436ecf… wyoung 181 { "optimum", 0 },
0652717… stephan 182 { "rowspan", AMSK_ROWSPAN },
0652717… stephan 183 { "size", AMSK_SIZE },
6436ecf… wyoung 184 { "sizes", 0 },
0652717… stephan 185 { "src", AMSK_SRC },
6436ecf… wyoung 186 { "srcset", 0 },
0652717… stephan 187 { "start", AMSK_START },
0652717… stephan 188 { "style", AMSK_STYLE },
0652717… stephan 189 { "target", AMSK_TARGET },
b8078ae… drh 190 { "title", AMSK_TITLE },
0652717… stephan 191 { "type", AMSK_TYPE },
0652717… stephan 192 { "valign", AMSK_VALIGN },
0652717… stephan 193 { "value", AMSK_VALUE },
0652717… stephan 194 { "vspace", AMSK_VSPACE },
0652717… stephan 195 { "width", AMSK_WIDTH },
dbda8d6… drh 196 };
dbda8d6… drh 197
dbda8d6… drh 198 /*
dbda8d6… drh 199 ** Use binary search to locate a tag in the aAttribute[] table.
dbda8d6… drh 200 */
dbda8d6… drh 201 static int findAttr(const char *z){
dbda8d6… drh 202 int i, c, first, last;
d0305b3… aku 203 first = 1;
3cb9ba4… andygoth 204 last = count(aAttribute) - 1;
dbda8d6… drh 205 while( first<=last ){
dbda8d6… drh 206 i = (first+last)/2;
32ad9a1… drh 207 c = fossil_strcmp(aAttribute[i].zName, z);
dbda8d6… drh 208 if( c==0 ){
d0305b3… aku 209 return i;
dbda8d6… drh 210 }else if( c<0 ){
dbda8d6… drh 211 first = i+1;
dbda8d6… drh 212 }else{
dbda8d6… drh 213 last = i-1;
dbda8d6… drh 214 }
dbda8d6… drh 215 }
dbda8d6… drh 216 return 0;
dbda8d6… drh 217 }
dbda8d6… drh 218
dbda8d6… drh 219
dbda8d6… drh 220
dbda8d6… drh 221 /*
dbda8d6… drh 222 ** Allowed markup.
dbda8d6… drh 223 **
dbda8d6… drh 224 ** Except for MARKUP_INVALID, this must all be in alphabetical order
dbda8d6… drh 225 ** and in numerical sequence. The first markup type must be zero.
b9d3b42… drh 226 ** The value for MARKUP_XYZ must correspond to the <xyz> entry
11064e4… stephan 227 ** in aMarkup[].
11064e4… stephan 228 */
81deb15… stephan 229 enum markup_t {
81deb15… stephan 230 MARKUP_INVALID = 0,
81deb15… stephan 231 MARKUP_A,
81deb15… stephan 232 MARKUP_ABBR,
81deb15… stephan 233 MARKUP_ADDRESS,
81deb15… stephan 234 MARKUP_HTML5_ARTICLE,
81deb15… stephan 235 MARKUP_HTML5_ASIDE,
81deb15… stephan 236 MARKUP_B,
81deb15… stephan 237 MARKUP_BIG,
81deb15… stephan 238 MARKUP_BLOCKQUOTE,
81deb15… stephan 239 MARKUP_BR,
6436ecf… wyoung 240 MARKUP_CAPTION,
81deb15… stephan 241 MARKUP_CENTER,
81deb15… stephan 242 MARKUP_CITE,
81deb15… stephan 243 MARKUP_CODE,
81deb15… stephan 244 MARKUP_COL,
81deb15… stephan 245 MARKUP_COLGROUP,
81deb15… stephan 246 MARKUP_DD,
81deb15… stephan 247 MARKUP_DEL,
35df3e6… stephan 248 MARKUP_DETAILS,
81deb15… stephan 249 MARKUP_DFN,
81deb15… stephan 250 MARKUP_DIV,
81deb15… stephan 251 MARKUP_DL,
81deb15… stephan 252 MARKUP_DT,
81deb15… stephan 253 MARKUP_EM,
6436ecf… wyoung 254 MARKUP_FIGCAPTION,
6436ecf… wyoung 255 MARKUP_FIGURE,
81deb15… stephan 256 MARKUP_FONT,
81deb15… stephan 257 MARKUP_HTML5_FOOTER,
81deb15… stephan 258 MARKUP_H1,
81deb15… stephan 259 MARKUP_H2,
81deb15… stephan 260 MARKUP_H3,
81deb15… stephan 261 MARKUP_H4,
81deb15… stephan 262 MARKUP_H5,
81deb15… stephan 263 MARKUP_H6,
81deb15… stephan 264 MARKUP_HTML5_HEADER,
81deb15… stephan 265 MARKUP_HR,
81deb15… stephan 266 MARKUP_I,
81deb15… stephan 267 MARKUP_IMG,
81deb15… stephan 268 MARKUP_INS,
81deb15… stephan 269 MARKUP_KBD,
6436ecf… wyoung 270 MARKUP_LABEL,
81deb15… stephan 271 MARKUP_LI,
6436ecf… wyoung 272 MARKUP_MARK,
6436ecf… wyoung 273 MARKUP_METER,
81deb15… stephan 274 MARKUP_HTML5_NAV,
81deb15… stephan 275 MARKUP_NOBR,
81deb15… stephan 276 MARKUP_NOWIKI,
81deb15… stephan 277 MARKUP_OL,
81deb15… stephan 278 MARKUP_P,
6436ecf… wyoung 279 MARKUP_PICTURE,
81deb15… stephan 280 MARKUP_PRE,
6436ecf… wyoung 281 MARKUP_PROGRESS,
6436ecf… wyoung 282 MARKUP_Q,
81deb15… stephan 283 MARKUP_S,
81deb15… stephan 284 MARKUP_SAMP,
81deb15… stephan 285 MARKUP_HTML5_SECTION,
81deb15… stephan 286 MARKUP_SMALL,
6436ecf… wyoung 287 MARKUP_SOURCE,
81deb15… stephan 288 MARKUP_SPAN,
81deb15… stephan 289 MARKUP_STRIKE,
81deb15… stephan 290 MARKUP_STRONG,
81deb15… stephan 291 MARKUP_SUB,
35df3e6… stephan 292 MARKUP_SUMMARY,
81deb15… stephan 293 MARKUP_SUP,
81deb15… stephan 294 MARKUP_TABLE,
81deb15… stephan 295 MARKUP_TBODY,
81deb15… stephan 296 MARKUP_TD,
81deb15… stephan 297 MARKUP_TFOOT,
81deb15… stephan 298 MARKUP_TH,
81deb15… stephan 299 MARKUP_THEAD,
6436ecf… wyoung 300 MARKUP_TIME,
81deb15… stephan 301 MARKUP_TITLE,
81deb15… stephan 302 MARKUP_TR,
81deb15… stephan 303 MARKUP_TT,
81deb15… stephan 304 MARKUP_U,
81deb15… stephan 305 MARKUP_UL,
81deb15… stephan 306 MARKUP_VAR,
a8c66b1… wyoung 307 MARKUP_VERBATIM,
a8c66b1… wyoung 308 MARKUP_WBR
81deb15… stephan 309 };
dbda8d6… drh 310
dbda8d6… drh 311 /*
dbda8d6… drh 312 ** The various markup is divided into the following types:
dbda8d6… drh 313 */
dbda8d6… drh 314 #define MUTYPE_SINGLE 0x0001 /* <img>, <br>, or <hr> */
dbda8d6… drh 315 #define MUTYPE_BLOCK 0x0002 /* Forms a new paragraph. ex: <p>, <h2> */
dbda8d6… drh 316 #define MUTYPE_FONT 0x0004 /* Font changes. ex: <b>, <font>, <sub> */
dbda8d6… drh 317 #define MUTYPE_LIST 0x0010 /* Lists. <ol>, <ul>, or <dl> */
dbda8d6… drh 318 #define MUTYPE_LI 0x0020 /* List items. <li>, <dd>, <dt> */
dbda8d6… drh 319 #define MUTYPE_TABLE 0x0040 /* <table> */
dbda8d6… drh 320 #define MUTYPE_TR 0x0080 /* <tr> */
dbda8d6… drh 321 #define MUTYPE_TD 0x0100 /* <td> or <th> */
dbda8d6… drh 322 #define MUTYPE_SPECIAL 0x0200 /* <nowiki> or <verbatim> */
dbda8d6… drh 323 #define MUTYPE_HYPERLINK 0x0400 /* <a> */
a7807a8… drh 324
a7807a8… drh 325 /* MUTYPE values for elements that require strictly nested end-tags */
a7807a8… drh 326 #define MUTYPE_Nested 0x0656
d0305b3… aku 327
d0305b3… aku 328 /*
d0305b3… aku 329 ** These markup types must have an end tag.
d0305b3… aku 330 */
dbda8d6… drh 331 #define MUTYPE_STACK (MUTYPE_BLOCK | MUTYPE_FONT | MUTYPE_LIST | MUTYPE_TABLE)
d0305b3… aku 332
d0305b3… aku 333 /*
d0305b3… aku 334 ** This markup types are allowed for "inline" text.
d0305b3… aku 335 */
d0305b3… aku 336 #define MUTYPE_INLINE (MUTYPE_FONT | MUTYPE_HYPERLINK)
dbda8d6… drh 337
dbda8d6… drh 338 static const struct AllowedMarkup {
dbda8d6… drh 339 const char *zName; /* Name of the markup */
dbda8d6… drh 340 char iCode; /* The MARKUP_* code */
dbda8d6… drh 341 short int iType; /* The MUTYPE_* code */
6436ecf… wyoung 342 amsk_t allowedAttr; /* Allowed attributes on this markup */
dbda8d6… drh 343 } aMarkup[] = {
d0305b3… aku 344 { 0, MARKUP_INVALID, 0, 0 },
d0305b3… aku 345 { "a", MARKUP_A, MUTYPE_HYPERLINK,
b8078ae… drh 346 AMSK_HREF|AMSK_NAME|AMSK_CLASS|AMSK_TARGET|AMSK_STYLE|
b8078ae… drh 347 AMSK_TITLE},
11064e4… stephan 348 { "abbr", MARKUP_ABBR, MUTYPE_FONT,
81deb15… stephan 349 AMSK_ID|AMSK_CLASS|AMSK_STYLE|AMSK_TITLE },
717cd27… drh 350 { "address", MARKUP_ADDRESS, MUTYPE_BLOCK, AMSK_STYLE },
93dffb9… stephan 351 { "article", MARKUP_HTML5_ARTICLE, MUTYPE_BLOCK,
35df3e6… stephan 352 AMSK_ID|AMSK_CLASS|AMSK_STYLE },
93dffb9… stephan 353 { "aside", MARKUP_HTML5_ASIDE, MUTYPE_BLOCK,
35df3e6… stephan 354 AMSK_ID|AMSK_CLASS|AMSK_STYLE },
717cd27… drh 355 { "b", MARKUP_B, MUTYPE_FONT, AMSK_STYLE },
717cd27… drh 356 { "big", MARKUP_BIG, MUTYPE_FONT, AMSK_STYLE },
717cd27… drh 357 { "blockquote", MARKUP_BLOCKQUOTE, MUTYPE_BLOCK, AMSK_STYLE },
717cd27… drh 358 { "br", MARKUP_BR, MUTYPE_SINGLE, AMSK_CLEAR },
6436ecf… wyoung 359 { "caption", MARKUP_CAPTION, MUTYPE_BLOCK,
6436ecf… wyoung 360 AMSK_ID|AMSK_CLASS|AMSK_STYLE },
717cd27… drh 361 { "center", MARKUP_CENTER, MUTYPE_BLOCK, AMSK_STYLE },
717cd27… drh 362 { "cite", MARKUP_CITE, MUTYPE_FONT, AMSK_STYLE },
717cd27… drh 363 { "code", MARKUP_CODE, MUTYPE_FONT, AMSK_STYLE },
172dccb… renez 364 { "col", MARKUP_COL, MUTYPE_SINGLE,
717cd27… drh 365 AMSK_ALIGN|AMSK_CLASS|AMSK_COLSPAN|AMSK_WIDTH|AMSK_STYLE },
172dccb… renez 366 { "colgroup", MARKUP_COLGROUP, MUTYPE_BLOCK,
717cd27… drh 367 AMSK_ALIGN|AMSK_CLASS|AMSK_COLSPAN|AMSK_WIDTH|AMSK_STYLE},
717cd27… drh 368 { "dd", MARKUP_DD, MUTYPE_LI, AMSK_STYLE },
ae9a9db… drh 369 { "del", MARKUP_DEL, MUTYPE_FONT, AMSK_STYLE },
35df3e6… stephan 370 { "details", MARKUP_DETAILS, MUTYPE_BLOCK,
6436ecf… wyoung 371 AMSK_ID|AMSK_CLASS|AMSK_STYLE|AMSK_OPEN },
717cd27… drh 372 { "dfn", MARKUP_DFN, MUTYPE_FONT, AMSK_STYLE },
717cd27… drh 373 { "div", MARKUP_DIV, MUTYPE_BLOCK,
717cd27… drh 374 AMSK_ID|AMSK_CLASS|AMSK_STYLE },
717cd27… drh 375 { "dl", MARKUP_DL, MUTYPE_LIST,
717cd27… drh 376 AMSK_COMPACT|AMSK_STYLE },
717cd27… drh 377 { "dt", MARKUP_DT, MUTYPE_LI, AMSK_STYLE },
717cd27… drh 378 { "em", MARKUP_EM, MUTYPE_FONT, AMSK_STYLE },
6436ecf… wyoung 379 { "figcaption", MARKUP_FIGCAPTION, MUTYPE_BLOCK,
6436ecf… wyoung 380 AMSK_ID|AMSK_CLASS|AMSK_STYLE },
6436ecf… wyoung 381 { "figure", MARKUP_FIGURE, MUTYPE_BLOCK,
6436ecf… wyoung 382 AMSK_ID|AMSK_CLASS|AMSK_STYLE },
dbda8d6… drh 383 { "font", MARKUP_FONT, MUTYPE_FONT,
717cd27… drh 384 AMSK_COLOR|AMSK_FACE|AMSK_SIZE|AMSK_STYLE },
93dffb9… stephan 385 { "footer", MARKUP_HTML5_FOOTER, MUTYPE_BLOCK,
35df3e6… stephan 386 AMSK_ID|AMSK_CLASS|AMSK_STYLE },
717cd27… drh 387 { "h1", MARKUP_H1, MUTYPE_BLOCK,
717cd27… drh 388 AMSK_ALIGN|AMSK_CLASS|AMSK_STYLE },
717cd27… drh 389 { "h2", MARKUP_H2, MUTYPE_BLOCK,
717cd27… drh 390 AMSK_ALIGN|AMSK_CLASS|AMSK_STYLE },
717cd27… drh 391 { "h3", MARKUP_H3, MUTYPE_BLOCK,
717cd27… drh 392 AMSK_ALIGN|AMSK_CLASS|AMSK_STYLE },
717cd27… drh 393 { "h4", MARKUP_H4, MUTYPE_BLOCK,
717cd27… drh 394 AMSK_ALIGN|AMSK_CLASS|AMSK_STYLE },
717cd27… drh 395 { "h5", MARKUP_H5, MUTYPE_BLOCK,
717cd27… drh 396 AMSK_ALIGN|AMSK_CLASS|AMSK_STYLE },
717cd27… drh 397 { "h6", MARKUP_H6, MUTYPE_BLOCK,
717cd27… drh 398 AMSK_ALIGN|AMSK_CLASS|AMSK_STYLE },
93dffb9… stephan 399 { "header", MARKUP_HTML5_HEADER, MUTYPE_BLOCK,
35df3e6… stephan 400 AMSK_ID|AMSK_CLASS|AMSK_STYLE },
b9d3b42… drh 401 { "hr", MARKUP_HR, MUTYPE_SINGLE,
717cd27… drh 402 AMSK_ALIGN|AMSK_COLOR|AMSK_SIZE|AMSK_WIDTH|
717cd27… drh 403 AMSK_STYLE|AMSK_CLASS },
717cd27… drh 404 { "i", MARKUP_I, MUTYPE_FONT, AMSK_STYLE },
b9d3b42… drh 405 { "img", MARKUP_IMG, MUTYPE_SINGLE,
e01aa8c… drh 406 AMSK_ALIGN|AMSK_ALT|AMSK_BORDER|AMSK_HEIGHT|
6436ecf… wyoung 407 AMSK_HSPACE|AMSK_SRC|AMSK_VSPACE|AMSK_WIDTH|AMSK_STYLE },
ae9a9db… drh 408 { "ins", MARKUP_INS, MUTYPE_FONT, AMSK_STYLE },
717cd27… drh 409 { "kbd", MARKUP_KBD, MUTYPE_FONT, AMSK_STYLE },
6436ecf… wyoung 410 { "label", MARKUP_LABEL, MUTYPE_FONT,
6436ecf… wyoung 411 AMSK_ID|AMSK_CLASS|AMSK_STYLE },
b9d3b42… drh 412 { "li", MARKUP_LI, MUTYPE_LI,
717cd27… drh 413 AMSK_TYPE|AMSK_VALUE|AMSK_STYLE },
6436ecf… wyoung 414 { "mark", MARKUP_MARK, MUTYPE_FONT,
6436ecf… wyoung 415 AMSK_ID|AMSK_CLASS|AMSK_STYLE },
6436ecf… wyoung 416 { "meter", MARKUP_METER, MUTYPE_FONT,
6436ecf… wyoung 417 AMSK_ID|AMSK_CLASS|AMSK_STYLE|AMSK_VALUE },
93dffb9… stephan 418 { "nav", MARKUP_HTML5_NAV, MUTYPE_BLOCK,
35df3e6… stephan 419 AMSK_ID|AMSK_CLASS|AMSK_STYLE },
dbda8d6… drh 420 { "nobr", MARKUP_NOBR, MUTYPE_FONT, 0 },
dbda8d6… drh 421 { "nowiki", MARKUP_NOWIKI, MUTYPE_SPECIAL, 0 },
b9d3b42… drh 422 { "ol", MARKUP_OL, MUTYPE_LIST,
717cd27… drh 423 AMSK_START|AMSK_TYPE|AMSK_COMPACT|AMSK_STYLE },
717cd27… drh 424 { "p", MARKUP_P, MUTYPE_BLOCK,
717cd27… drh 425 AMSK_ALIGN|AMSK_CLASS|AMSK_STYLE },
6436ecf… wyoung 426 { "picture", MARKUP_PICTURE, MUTYPE_BLOCK,
6436ecf… wyoung 427 AMSK_ID|AMSK_CLASS|AMSK_STYLE },
717cd27… drh 428 { "pre", MARKUP_PRE, MUTYPE_BLOCK, AMSK_STYLE },
6436ecf… wyoung 429 { "progress", MARKUP_PROGRESS, MUTYPE_FONT,
6436ecf… wyoung 430 AMSK_ID|AMSK_CLASS|AMSK_STYLE|AMSK_VALUE },
6436ecf… wyoung 431 { "q", MARKUP_Q, MUTYPE_FONT,
6436ecf… wyoung 432 AMSK_CLASS|AMSK_STYLE|AMSK_CITE },
717cd27… drh 433 { "s", MARKUP_S, MUTYPE_FONT, AMSK_STYLE },
717cd27… drh 434 { "samp", MARKUP_SAMP, MUTYPE_FONT, AMSK_STYLE },
93dffb9… stephan 435 { "section", MARKUP_HTML5_SECTION, MUTYPE_BLOCK,
35df3e6… stephan 436 AMSK_ID|AMSK_CLASS|AMSK_STYLE },
717cd27… drh 437 { "small", MARKUP_SMALL, MUTYPE_FONT, AMSK_STYLE },
6436ecf… wyoung 438 { "source", MARKUP_SOURCE, MUTYPE_SINGLE,
6436ecf… wyoung 439 AMSK_TYPE|AMSK_ID|AMSK_CLASS|AMSK_STYLE },
717cd27… drh 440 { "span", MARKUP_SPAN, MUTYPE_BLOCK,
717cd27… drh 441 AMSK_ALIGN|AMSK_CLASS|AMSK_STYLE },
717cd27… drh 442 { "strike", MARKUP_STRIKE, MUTYPE_FONT, AMSK_STYLE },
717cd27… drh 443 { "strong", MARKUP_STRONG, MUTYPE_FONT, AMSK_STYLE },
717cd27… drh 444 { "sub", MARKUP_SUB, MUTYPE_FONT, AMSK_STYLE },
35df3e6… stephan 445 { "summary", MARKUP_SUMMARY, MUTYPE_BLOCK,
35df3e6… stephan 446 AMSK_ALIGN|AMSK_CLASS|AMSK_STYLE },
717cd27… drh 447 { "sup", MARKUP_SUP, MUTYPE_FONT, AMSK_STYLE },
b9d3b42… drh 448 { "table", MARKUP_TABLE, MUTYPE_TABLE,
e01aa8c… drh 449 AMSK_ALIGN|AMSK_BGCOLOR|AMSK_BORDER|AMSK_CELLPADDING|
717cd27… drh 450 AMSK_CELLSPACING|AMSK_HSPACE|AMSK_VSPACE|AMSK_CLASS|
717cd27… drh 451 AMSK_STYLE },
717cd27… drh 452 { "tbody", MARKUP_TBODY, MUTYPE_BLOCK,
717cd27… drh 453 AMSK_ALIGN|AMSK_CLASS|AMSK_STYLE },
b9d3b42… drh 454 { "td", MARKUP_TD, MUTYPE_TD,
e01aa8c… drh 455 AMSK_ALIGN|AMSK_BGCOLOR|AMSK_COLSPAN|
717cd27… drh 456 AMSK_ROWSPAN|AMSK_VALIGN|AMSK_CLASS|AMSK_STYLE },
70b4f10… jan.nijtmans 457 { "tfoot", MARKUP_TFOOT, MUTYPE_BLOCK,
717cd27… drh 458 AMSK_ALIGN|AMSK_CLASS|AMSK_STYLE },
dbda8d6… drh 459 { "th", MARKUP_TH, MUTYPE_TD,
e01aa8c… drh 460 AMSK_ALIGN|AMSK_BGCOLOR|AMSK_COLSPAN|
717cd27… drh 461 AMSK_ROWSPAN|AMSK_VALIGN|AMSK_CLASS|AMSK_STYLE },
717cd27… drh 462 { "thead", MARKUP_THEAD, MUTYPE_BLOCK,
717cd27… drh 463 AMSK_ALIGN|AMSK_CLASS|AMSK_STYLE },
6436ecf… wyoung 464 { "time", MARKUP_TIME, MUTYPE_FONT,
6436ecf… wyoung 465 AMSK_DATETIME|AMSK_ID|AMSK_CLASS|AMSK_STYLE|AMSK_TITLE },
d8586b5… drh 466 { "title", MARKUP_TITLE, MUTYPE_BLOCK, 0 },
b9d3b42… drh 467 { "tr", MARKUP_TR, MUTYPE_TR,
717cd27… drh 468 AMSK_ALIGN|AMSK_BGCOLOR|AMSK_VALIGN|AMSK_CLASS|AMSK_STYLE },
717cd27… drh 469 { "tt", MARKUP_TT, MUTYPE_FONT, AMSK_STYLE },
717cd27… drh 470 { "u", MARKUP_U, MUTYPE_FONT, AMSK_STYLE },
b9d3b42… drh 471 { "ul", MARKUP_UL, MUTYPE_LIST,
717cd27… drh 472 AMSK_TYPE|AMSK_COMPACT|AMSK_STYLE },
717cd27… drh 473 { "var", MARKUP_VAR, MUTYPE_FONT, AMSK_STYLE },
717cd27… drh 474 { "verbatim", MARKUP_VERBATIM, MUTYPE_SPECIAL,
717cd27… drh 475 AMSK_ID|AMSK_TYPE },
a8c66b1… wyoung 476 { "wbr", MARKUP_WBR, MUTYPE_SINGLE, 0 },
dbda8d6… drh 477 };
c492eab… wolfgang 478
c492eab… wolfgang 479 void show_allowed_wiki_markup( void ){
c492eab… wolfgang 480 int i; /* loop over allowedAttr */
3cb9ba4… andygoth 481 for( i=1 ; i<=count(aMarkup) - 1 ; i++ ){
c492eab… wolfgang 482 @ &lt;%s(aMarkup[i].zName)&gt;
c492eab… wolfgang 483 }
c492eab… wolfgang 484 }
dbda8d6… drh 485
dbda8d6… drh 486 /*
dbda8d6… drh 487 ** Use binary search to locate a tag in the aMarkup[] table.
dbda8d6… drh 488 */
dbda8d6… drh 489 static int findTag(const char *z){
dbda8d6… drh 490 int i, c, first, last;
d0305b3… aku 491 first = 1;
3cb9ba4… andygoth 492 last = count(aMarkup) - 1;
dbda8d6… drh 493 while( first<=last ){
dbda8d6… drh 494 i = (first+last)/2;
32ad9a1… drh 495 c = fossil_strcmp(aMarkup[i].zName, z);
dbda8d6… drh 496 if( c==0 ){
dbda8d6… drh 497 assert( aMarkup[i].iCode==i );
dbda8d6… drh 498 return i;
dbda8d6… drh 499 }else if( c<0 ){
dbda8d6… drh 500 first = i+1;
dbda8d6… drh 501 }else{
dbda8d6… drh 502 last = i-1;
dbda8d6… drh 503 }
dbda8d6… drh 504 }
dbda8d6… drh 505 return MARKUP_INVALID;
dbda8d6… drh 506 }
dbda8d6… drh 507
dbda8d6… drh 508 /*
dbda8d6… drh 509 ** Token types
dbda8d6… drh 510 */
3243e63… drh 511 #define TOKEN_MARKUP 1 /* <...> */
3243e63… drh 512 #define TOKEN_CHARACTER 2 /* "&" or "<" not part of markup */
3243e63… drh 513 #define TOKEN_LINK 3 /* [...] */
3243e63… drh 514 #define TOKEN_PARAGRAPH 4 /* blank lines */
3243e63… drh 515 #define TOKEN_NEWLINE 5 /* A single "\n" */
3243e63… drh 516 #define TOKEN_BUL_LI 6 /* " * " */
3243e63… drh 517 #define TOKEN_NUM_LI 7 /* " # " */
3243e63… drh 518 #define TOKEN_ENUM 8 /* " \(?\d+[.)]? " */
3243e63… drh 519 #define TOKEN_INDENT 9 /* " " */
3243e63… drh 520 #define TOKEN_RAW 10 /* Output exactly (used when wiki-use-html==1) */
3243e63… drh 521 #define TOKEN_TEXT 11 /* None of the above */
dbda8d6… drh 522
dbda8d6… drh 523 /*
18c310a… drh 524 ** State flags. Save the lower 16 bits for the WIKI_* flags.
dbda8d6… drh 525 */
18c310a… drh 526 #define AT_NEWLINE 0x0010000 /* At start of a line */
18c310a… drh 527 #define AT_PARAGRAPH 0x0020000 /* At start of a paragraph */
18c310a… drh 528 #define ALLOW_WIKI 0x0040000 /* Allow wiki markup */
bf67db0… drh 529 #define ALLOW_LINKS 0x0080000 /* Allow [...] hyperlinks */
bf67db0… drh 530 #define FONT_MARKUP_ONLY 0x0100000 /* Only allow MUTYPE_FONT markup */
0432c38… drh 531 #define IN_LIST 0x0200000 /* Within wiki <ul> or <ol> */
d0305b3… aku 532
d0305b3… aku 533 /*
d0305b3… aku 534 ** Current state of the rendering engine
d0305b3… aku 535 */
d0305b3… aku 536 typedef struct Renderer Renderer;
d0305b3… aku 537 struct Renderer {
d0305b3… aku 538 Blob *pOut; /* Output appended to this blob */
d0305b3… aku 539 int state; /* Flag that govern rendering */
53b080b… drh 540 int mRender; /* Mask of RENDER_* values to return */
90676f4… drh 541 unsigned renderFlags; /* Flags from the client */
d0305b3… aku 542 int wikiList; /* Current wiki list type */
d0305b3… aku 543 int inVerbatim; /* True in <verbatim> mode */
d0305b3… aku 544 int preVerbState; /* Value of state prior to verbatim */
d0305b3… aku 545 int wantAutoParagraph; /* True if a <p> is desired */
d0305b3… aku 546 int inAutoParagraph; /* True if within an automatic paragraph */
aac2a35… stephan 547 int pikchrHtmlFlags; /* Flags for pikchr_to_html() */
d0305b3… aku 548 const char *zVerbatimId; /* The id= attribute of <verbatim> */
d0305b3… aku 549 int nStack; /* Number of elements on the stack */
d0305b3… aku 550 int nAlloc; /* Space allocated for aStack */
e01aa8c… drh 551 struct sStack {
e01aa8c… drh 552 short iCode; /* Markup code */
e01aa8c… drh 553 short allowWiki; /* ALLOW_WIKI if wiki allowed before tag */
e01aa8c… drh 554 const char *zId; /* ID attribute or NULL */
e01aa8c… drh 555 } *aStack;
d0305b3… aku 556 };
d0305b3… aku 557
7c2577b… drh 558 /*
7c2577b… drh 559 ** Return TRUE if HTML should be used as the sole markup language for wiki.
7c2577b… drh 560 **
7c2577b… drh 561 ** On first invocation, this routine consults the "wiki-use-html" setting.
7c2577b… drh 562 ** It caches the result for subsequent invocations, under the assumption
7c2577b… drh 563 ** that the setting will not change.
7c2577b… drh 564 */
7c2577b… drh 565 static int wikiUsesHtml(void){
7c2577b… drh 566 static int r = -1;
7c2577b… drh 567 if( r<0 ) r = db_get_boolean("wiki-use-html", 0);
7c2577b… drh 568 return r;
7c2577b… drh 569 }
dbda8d6… drh 570
dbda8d6… drh 571 /*
dbda8d6… drh 572 ** z points to a "<" character. Check to see if this is the start of
dbda8d6… drh 573 ** a valid markup. If it is, return the total number of characters in
dbda8d6… drh 574 ** the markup including the initial "<" and the terminating ">". If
dbda8d6… drh 575 ** it is not well-formed markup, return 0.
dbda8d6… drh 576 */
3f99bca… drh 577 int html_tag_length(const char *z){
dbda8d6… drh 578 int n = 1;
dbda8d6… drh 579 int inparen = 0;
96722b6… drh 580 int c;
dbda8d6… drh 581 if( z[n]=='/' ){ n++; }
2fac809… drh 582 if( !fossil_isalpha(z[n]) ) return 0;
4f8c897… drh 583 while( fossil_isalnum(z[n]) || z[n]=='-' ){ n++; }
ee78a38… drh 584 c = z[n];
ee78a38… drh 585 if( c=='/' && z[n+1]=='>' ){ return n+2; }
2fac809… drh 586 if( c!='>' && !fossil_isspace(c) ) return 0;
96722b6… drh 587 while( (c = z[n])!=0 && (c!='>' || inparen) ){
96722b6… drh 588 if( c==inparen ){
96722b6… drh 589 inparen = 0;
ee78a38… drh 590 }else if( inparen==0 && (c=='"' || c=='\'') ){
96722b6… drh 591 inparen = c;
dbda8d6… drh 592 }
bbcb632… aku 593 n++;
dbda8d6… drh 594 }
dbda8d6… drh 595 if( z[n]!='>' ) return 0;
dbda8d6… drh 596 return n+1;
dbda8d6… drh 597 }
dbda8d6… drh 598
dbda8d6… drh 599 /*
dbda8d6… drh 600 ** z points to a "\n" character. Check to see if this newline is
dbda8d6… drh 601 ** followed by one or more blank lines. If it is, return the number
dbda8d6… drh 602 ** of characters through the closing "\n". If not, return 0.
dbda8d6… drh 603 */
dbda8d6… drh 604 static int paragraphBreakLength(const char *z){
dbda8d6… drh 605 int i, n;
dbda8d6… drh 606 int nNewline = 1;
2fac809… drh 607 for(i=1, n=0; fossil_isspace(z[i]); i++){
dbda8d6… drh 608 if( z[i]=='\n' ){
dbda8d6… drh 609 nNewline++;
dbda8d6… drh 610 n = i;
dbda8d6… drh 611 }
dbda8d6… drh 612 }
dbda8d6… drh 613 if( nNewline>=2 ){
dbda8d6… drh 614 return n+1;
dbda8d6… drh 615 }else{
dbda8d6… drh 616 return 0;
dbda8d6… drh 617 }
dbda8d6… drh 618 }
dbda8d6… drh 619
dbda8d6… drh 620 /*
dbda8d6… drh 621 ** Return the number of characters until the next "interesting"
dbda8d6… drh 622 ** characters.
dbda8d6… drh 623 **
dbda8d6… drh 624 ** Interesting characters are:
dbda8d6… drh 625 **
dbda8d6… drh 626 ** <
dbda8d6… drh 627 ** &
dbda8d6… drh 628 ** \n
dbda8d6… drh 629 ** [
dbda8d6… drh 630 **
bf67db0… drh 631 ** The "[" is only considered if flags contain ALLOW_LINKS or ALLOW_WIKI.
e2bdc10… danield 632 ** The "\n" is only considered interesting if the flags contains ALLOW_WIKI.
dbda8d6… drh 633 */
bf67db0… drh 634 static int textLength(const char *z, int flags){
64a6346… drh 635 const char *zReject;
bf67db0… drh 636 if( flags & ALLOW_WIKI ){
64a6346… drh 637 zReject = "<&[\n";
bf67db0… drh 638 }else if( flags & ALLOW_LINKS ){
64a6346… drh 639 zReject = "<&[";
bf67db0… drh 640 }else{
64a6346… drh 641 zReject = "<&";
bf67db0… drh 642 }
64a6346… drh 643 return strcspn(z, zReject);
dbda8d6… drh 644 }
dbda8d6… drh 645
dbda8d6… drh 646 /*
dbda8d6… drh 647 ** Return true if z[] begins with an HTML character element.
dbda8d6… drh 648 */
dbda8d6… drh 649 static int isElement(const char *z){
dbda8d6… drh 650 int i;
dbda8d6… drh 651 assert( z[0]=='&' );
dbda8d6… drh 652 if( z[1]=='#' ){
2fac809… drh 653 for(i=2; fossil_isdigit(z[i]); i++){}
dbda8d6… drh 654 return i>2 && z[i]==';';
dbda8d6… drh 655 }else{
2fac809… drh 656 for(i=1; fossil_isalpha(z[i]); i++){}
dbda8d6… drh 657 return i>1 && z[i]==';';
dbda8d6… drh 658 }
dbda8d6… drh 659 }
dbda8d6… drh 660
dbda8d6… drh 661 /*
2bb1d64… jeremy_c 662 ** Check to see if the z[] string is the beginning of a wiki list item.
d0305b3… aku 663 ** If it is, return the length of the bullet text. Otherwise return 0.
d0305b3… aku 664 */
2bb1d64… jeremy_c 665 static int listItemLength(const char *z, const char listChar){
d0305b3… aku 666 int i, n;
d0305b3… aku 667 n = 0;
d0305b3… aku 668 i = 0;
d0305b3… aku 669 while( z[n]==' ' || z[n]=='\t' ){
d0305b3… aku 670 if( z[n]=='\t' ) i++;
d0305b3… aku 671 i++;
d0305b3… aku 672 n++;
d0305b3… aku 673 }
2bb1d64… jeremy_c 674 if( i<2 || z[n]!=listChar ) return 0;
d0305b3… aku 675 n++;
d0305b3… aku 676 i = 0;
d0305b3… aku 677 while( z[n]==' ' || z[n]=='\t' ){
d0305b3… aku 678 if( z[n]=='\t' ) i++;
d0305b3… aku 679 i++;
d0305b3… aku 680 n++;
d0305b3… aku 681 }
2fac809… drh 682 if( i<2 || fossil_isspace(z[n]) ) return 0;
d0305b3… aku 683 return n;
d0305b3… aku 684 }
d0305b3… aku 685
d0305b3… aku 686 /*
99a319b… wyoung 687 ** Check to see if the z[] string is the beginning of an enumeration value.
dbda8d6… drh 688 ** If it is, return the length of the bullet text. Otherwise return 0.
d0305b3… aku 689 **
d0305b3… aku 690 ** Syntax:
d0305b3… aku 691 ** * a tab or two or more spaces
d0305b3… aku 692 ** * one or more digits
d0305b3… aku 693 ** * optional "."
d0305b3… aku 694 ** * another tab or two ore more spaces.
d0305b3… aku 695 **
dbda8d6… drh 696 */
d0305b3… aku 697 static int enumLength(const char *z){
dbda8d6… drh 698 int i, n;
dbda8d6… drh 699 n = 0;
dbda8d6… drh 700 i = 0;
dbda8d6… drh 701 while( z[n]==' ' || z[n]=='\t' ){
dbda8d6… drh 702 if( z[n]=='\t' ) i++;
dbda8d6… drh 703 i++;
dbda8d6… drh 704 n++;
dbda8d6… drh 705 }
d0305b3… aku 706 if( i<2 ) return 0;
2fac809… drh 707 for(i=0; fossil_isdigit(z[n]); i++, n++){}
d0305b3… aku 708 if( i==0 ) return 0;
d0305b3… aku 709 if( z[n]=='.' ){
d0305b3… aku 710 n++;
d0305b3… aku 711 }
dbda8d6… drh 712 i = 0;
dbda8d6… drh 713 while( z[n]==' ' || z[n]=='\t' ){
dbda8d6… drh 714 if( z[n]=='\t' ) i++;
dbda8d6… drh 715 i++;
dbda8d6… drh 716 n++;
dbda8d6… drh 717 }
2fac809… drh 718 if( i<2 || fossil_isspace(z[n]) ) return 0;
dbda8d6… drh 719 return n;
dbda8d6… drh 720 }
dbda8d6… drh 721
dbda8d6… drh 722 /*
dbda8d6… drh 723 ** Check to see if the z[] string is the beginning of an indented
dbda8d6… drh 724 ** paragraph. If it is, return the length of the indent. Otherwise
dbda8d6… drh 725 ** return 0.
dbda8d6… drh 726 */
dbda8d6… drh 727 static int indentLength(const char *z){
dbda8d6… drh 728 int i, n;
dbda8d6… drh 729 n = 0;
dbda8d6… drh 730 i = 0;
dbda8d6… drh 731 while( z[n]==' ' || z[n]=='\t' ){
dbda8d6… drh 732 if( z[n]=='\t' ) i++;
dbda8d6… drh 733 i++;
dbda8d6… drh 734 n++;
dbda8d6… drh 735 }
2fac809… drh 736 if( i<2 || fossil_isspace(z[n]) ) return 0;
dbda8d6… drh 737 return n;
dbda8d6… drh 738 }
dbda8d6… drh 739
dbda8d6… drh 740 /*
dbda8d6… drh 741 ** Check to see if the z[] string is a wiki hyperlink. If it is,
dbda8d6… drh 742 ** return the length of the hyperlink. Otherwise return 0.
dbda8d6… drh 743 */
dbda8d6… drh 744 static int linkLength(const char *z){
dbda8d6… drh 745 int n;
dbda8d6… drh 746 assert( z[0]=='[' );
dbda8d6… drh 747 for(n=0; z[n] && z[n]!=']'; n++){}
dbda8d6… drh 748 if( z[n]==']' ){
dbda8d6… drh 749 return n+1;
dbda8d6… drh 750 }else{
dbda8d6… drh 751 return 0;
dbda8d6… drh 752 }
dbda8d6… drh 753 }
dbda8d6… drh 754
dbda8d6… drh 755 /*
cf3809c… jeremy_c 756 ** Get the next wiki token.
b9d3b42… drh 757 **
dbda8d6… drh 758 ** z points to the start of a token. Return the number of
dbda8d6… drh 759 ** characters in that token. Write the token type into *pTokenType.
dbda8d6… drh 760 */
cf3809c… jeremy_c 761 static int nextWikiToken(const char *z, Renderer *p, int *pTokenType){
dbda8d6… drh 762 int n;
dbda8d6… drh 763 if( z[0]=='<' ){
3f99bca… drh 764 n = html_tag_length(z);
dbda8d6… drh 765 if( n>0 ){
53b080b… drh 766 p->mRender |= RENDER_TAG;
dbda8d6… drh 767 *pTokenType = TOKEN_MARKUP;
dbda8d6… drh 768 return n;
dbda8d6… drh 769 }else{
53b080b… drh 770 p->mRender |= RENDER_BADTAG;
53b080b… drh 771 *pTokenType = TOKEN_CHARACTER;
53b080b… drh 772 return 1;
53b080b… drh 773 }
53b080b… drh 774 }
53b080b… drh 775 if( z[0]=='&' ){
53b080b… drh 776 p->mRender |= RENDER_ENTITY;
53b080b… drh 777 if( (p->inVerbatim || !isElement(z)) ){
dbda8d6… drh 778 *pTokenType = TOKEN_CHARACTER;
dbda8d6… drh 779 return 1;
dbda8d6… drh 780 }
dbda8d6… drh 781 }
d0305b3… aku 782 if( (p->state & ALLOW_WIKI)!=0 ){
dbda8d6… drh 783 if( z[0]=='\n' ){
dbda8d6… drh 784 n = paragraphBreakLength(z);
dbda8d6… drh 785 if( n>0 ){
dbda8d6… drh 786 *pTokenType = TOKEN_PARAGRAPH;
dbda8d6… drh 787 return n;
8af0255… drh 788 }else{
dbda8d6… drh 789 *pTokenType = TOKEN_NEWLINE;
dbda8d6… drh 790 return 1;
dbda8d6… drh 791 }
dbda8d6… drh 792 }
2fac809… drh 793 if( (p->state & AT_NEWLINE)!=0 && fossil_isspace(z[0]) ){
2bb1d64… jeremy_c 794 n = listItemLength(z, '*');
2bb1d64… jeremy_c 795 if( n>0 ){
2bb1d64… jeremy_c 796 *pTokenType = TOKEN_BUL_LI;
2bb1d64… jeremy_c 797 return n;
2bb1d64… jeremy_c 798 }
2bb1d64… jeremy_c 799 n = listItemLength(z, '#');
dbda8d6… drh 800 if( n>0 ){
2bb1d64… jeremy_c 801 *pTokenType = TOKEN_NUM_LI;
dbda8d6… drh 802 return n;
dbda8d6… drh 803 }
dbda8d6… drh 804 n = enumLength(z);
dbda8d6… drh 805 if( n>0 ){
dbda8d6… drh 806 *pTokenType = TOKEN_ENUM;
dbda8d6… drh 807 return n;
dbda8d6… drh 808 }
dbda8d6… drh 809 }
2fac809… drh 810 if( (p->state & AT_PARAGRAPH)!=0 && fossil_isspace(z[0]) ){
dbda8d6… drh 811 n = indentLength(z);
dbda8d6… drh 812 if( n>0 ){
dbda8d6… drh 813 *pTokenType = TOKEN_INDENT;
dbda8d6… drh 814 return n;
dbda8d6… drh 815 }
dbda8d6… drh 816 }
53b080b… drh 817 if( z[0]=='[' ){
53b080b… drh 818 if( (n = linkLength(z))>0 ){
53b080b… drh 819 *pTokenType = TOKEN_LINK;
53b080b… drh 820 return n;
f0d9fe2… drh 821 }else if( p->state & WIKI_MARK ){
f0d9fe2… drh 822 blob_append_string(p->pOut, "<mark>");
f0d9fe2… drh 823 p->mRender |= RENDER_BADLINK|RENDER_MARK;
53b080b… drh 824 }else{
53b080b… drh 825 p->mRender |= RENDER_BADLINK;
53b080b… drh 826 }
53b080b… drh 827 }
53b080b… drh 828 }else if( (p->state & ALLOW_LINKS)!=0 && z[0]=='[' ){
53b080b… drh 829 if( (n = linkLength(z))>0 ){
dbda8d6… drh 830 *pTokenType = TOKEN_LINK;
dbda8d6… drh 831 return n;
f0d9fe2… drh 832 }else if( p->state & WIKI_MARK ){
f0d9fe2… drh 833 blob_append_string(p->pOut, "<mark>");
f0d9fe2… drh 834 p->mRender |= RENDER_BADLINK|RENDER_MARK;
53b080b… drh 835 }else{
53b080b… drh 836 p->mRender |= RENDER_BADLINK;
dbda8d6… drh 837 }
dbda8d6… drh 838 }
dbda8d6… drh 839 *pTokenType = TOKEN_TEXT;
bf67db0… drh 840 return 1 + textLength(z+1, p->state);
cf3809c… jeremy_c 841 }
cf3809c… jeremy_c 842
cf3809c… jeremy_c 843 /*
cf3809c… jeremy_c 844 ** Parse only Wiki links, return everything else as TOKEN_RAW.
b9d3b42… drh 845 **
cf3809c… jeremy_c 846 ** z points to the start of a token. Return the number of
cf3809c… jeremy_c 847 ** characters in that token. Write the token type into *pTokenType.
cf3809c… jeremy_c 848 */
cf3809c… jeremy_c 849 static int nextRawToken(const char *z, Renderer *p, int *pTokenType){
cf3809c… jeremy_c 850 int n;
53b080b… drh 851 if( z[0]=='[' ){
53b080b… drh 852 if( (n = linkLength(z))>0 ){
53b080b… drh 853 *pTokenType = TOKEN_LINK;
53b080b… drh 854 return n;
d43f3e2… drh 855 }else if( p->state & WIKI_MARK ){
d43f3e2… drh 856 blob_append_string(p->pOut, "<mark>");
d43f3e2… drh 857 p->mRender |= RENDER_BADLINK|RENDER_MARK;
d43f3e2… drh 858 }else{
53b080b… drh 859 p->mRender |= RENDER_BADLINK;
53b080b… drh 860 }
cf3809c… jeremy_c 861 }
cf3809c… jeremy_c 862 *pTokenType = TOKEN_RAW;
cf3809c… jeremy_c 863 return 1 + textLength(z+1, p->state);
dbda8d6… drh 864 }
dbda8d6… drh 865
dbda8d6… drh 866 /*
dbda8d6… drh 867 ** A single markup is parsed into an instance of the following
dbda8d6… drh 868 ** structure.
dbda8d6… drh 869 */
dbda8d6… drh 870 typedef struct ParsedMarkup ParsedMarkup;
dbda8d6… drh 871 struct ParsedMarkup {
dbda8d6… drh 872 unsigned char endTag; /* True if </...> instead of <...> */
dbda8d6… drh 873 unsigned char iCode; /* MARKUP_* */
dbda8d6… drh 874 unsigned char nAttr; /* Number of attributes */
dbda8d6… drh 875 unsigned short iType; /* MUTYPE_* */
dbda8d6… drh 876 struct {
e01aa8c… drh 877 unsigned char iACode; /* ATTR_* */
dbda8d6… drh 878 char *zValue; /* Argument to this attribute. Might be NULL */
dbda8d6… drh 879 char cTerm; /* Original argument termination character */
dbda8d6… drh 880 } aAttr[10];
dbda8d6… drh 881 };
dbda8d6… drh 882
dbda8d6… drh 883 /*
6436ecf… wyoung 884 ** Return true if attribute iACode has already been parsed into p.
6436ecf… wyoung 885 */
6436ecf… wyoung 886 static int markupHasAttr(const ParsedMarkup *p, int iACode){
6436ecf… wyoung 887 int i;
6436ecf… wyoung 888 for(i=0; i<p->nAttr; i++){
6436ecf… wyoung 889 if( p->aAttr[i].iACode==iACode ) return 1;
6436ecf… wyoung 890 }
6436ecf… wyoung 891 return 0;
6436ecf… wyoung 892 }
6436ecf… wyoung 893
6436ecf… wyoung 894 /*
22c1ac4… drh 895 ** z[] is an HTML markup element - something that begins with '<'.
22c1ac4… drh 896 ** Parse this element into the p structure.
22c1ac4… drh 897 **
b9d3b42… drh 898 ** The content of z[] might be modified by converting characters
22c1ac4… drh 899 ** to lowercase and by inserting some "\000" characters.
dbda8d6… drh 900 */
6436ecf… wyoung 901 static amsk_t parseMarkup(ParsedMarkup *p, char *z){
a4d7e91… drh 902 int i, j, c;
e01aa8c… drh 903 int iACode;
a4d7e91… drh 904 char *zValue;
6436ecf… wyoung 905 amsk_t seen = 0;
a4d7e91… drh 906 char zTag[100];
dbda8d6… drh 907
dbda8d6… drh 908 if( z[1]=='/' ){
dbda8d6… drh 909 p->endTag = 1;
dbda8d6… drh 910 i = 2;
dbda8d6… drh 911 }else{
dbda8d6… drh 912 p->endTag = 0;
dbda8d6… drh 913 i = 1;
dbda8d6… drh 914 }
a4d7e91… drh 915 j = 0;
2fac809… drh 916 while( fossil_isalnum(z[i]) ){
53db40e… drh 917 if( j<(int)sizeof(zTag)-1 ) zTag[j++] = fossil_tolower(z[i]);
dbda8d6… drh 918 i++;
dbda8d6… drh 919 }
a4d7e91… drh 920 zTag[j] = 0;
dbda8d6… drh 921 p->iCode = findTag(zTag);
dbda8d6… drh 922 p->iType = aMarkup[p->iCode].iType;
dbda8d6… drh 923 p->nAttr = 0;
4f8c897… drh 924 c = 0;
4f8c897… drh 925 if( z[i]=='-' ){
4f8c897… drh 926 p->aAttr[0].iACode = iACode = ATTR_ID;
4f8c897… drh 927 i++;
4f8c897… drh 928 p->aAttr[0].zValue = &z[i];
4f8c897… drh 929 while( fossil_isalnum(z[i]) ){ i++; }
4f8c897… drh 930 p->aAttr[0].cTerm = c = z[i];
4f8c897… drh 931 z[i++] = 0;
4f8c897… drh 932 p->nAttr = 1;
a6e4dcb… drh 933 if( c=='>' ) return 0;
4f8c897… drh 934 }
2fac809… drh 935 while( fossil_isspace(z[i]) ){ i++; }
4f8c897… drh 936 while( c!='>' && p->nAttr<8 && fossil_isalpha(z[i]) ){
0652717… stephan 937 int attrOk; /* True to preserve attribute. False to ignore it */
a4d7e91… drh 938 j = 0;
2fac809… drh 939 while( fossil_isalnum(z[i]) ){
53db40e… drh 940 if( j<(int)sizeof(zTag)-1 ) zTag[j++] = fossil_tolower(z[i]);
dbda8d6… drh 941 i++;
dbda8d6… drh 942 }
a4d7e91… drh 943 zTag[j] = 0;
e01aa8c… drh 944 p->aAttr[p->nAttr].iACode = iACode = findAttr(zTag);
6436ecf… wyoung 945 attrOk = iACode!=0 && !markupHasAttr(p, iACode);
2fac809… drh 946 while( fossil_isspace(z[i]) ){ z++; }
dbda8d6… drh 947 if( z[i]!='=' ){
dbda8d6… drh 948 p->aAttr[p->nAttr].zValue = 0;
dbda8d6… drh 949 p->aAttr[p->nAttr].cTerm = 0;
dbda8d6… drh 950 c = 0;
dbda8d6… drh 951 }else{
dbda8d6… drh 952 i++;
2fac809… drh 953 while( fossil_isspace(z[i]) ){ z++; }
dbda8d6… drh 954 if( z[i]=='"' ){
dbda8d6… drh 955 i++;
dbda8d6… drh 956 zValue = &z[i];
dbda8d6… drh 957 while( z[i] && z[i]!='"' ){ i++; }
96722b6… drh 958 }else if( z[i]=='\'' ){
96722b6… drh 959 i++;
96722b6… drh 960 zValue = &z[i];
96722b6… drh 961 while( z[i] && z[i]!='\'' ){ i++; }
dbda8d6… drh 962 }else{
dbda8d6… drh 963 zValue = &z[i];
b07834b… drh 964 while( !fossil_isspace(z[i]) && z[i]!='>' ){
af616bc… drh 965 if( z[i]=='\'' || z[i]=='"' ) attrOk = 0;
af616bc… drh 966 i++;
b07834b… drh 967 }
dbda8d6… drh 968 }
71104b8… drh 969 if( attrOk ){
71104b8… drh 970 p->aAttr[p->nAttr].zValue = zValue;
71104b8… drh 971 p->aAttr[p->nAttr].cTerm = c = z[i];
48276cf… drh 972 if( z[i]==0 ){
48276cf… drh 973 i--;
48276cf… drh 974 }else{
48276cf… drh 975 z[i] = 0;
48276cf… drh 976 }
71104b8… drh 977 }
dbda8d6… drh 978 i++;
dbda8d6… drh 979 }
71104b8… drh 980 if( attrOk ){
6436ecf… wyoung 981 if( aAttribute[iACode].iMask ) seen |= aAttribute[iACode].iMask;
dbda8d6… drh 982 p->nAttr++;
dbda8d6… drh 983 }
2fac809… drh 984 while( fossil_isspace(z[i]) ){ i++; }
48276cf… drh 985 if( z[i]==0 || z[i]=='>' || (z[i]=='/' && z[i+1]=='>') ) break;
a6e4dcb… drh 986 }
a6e4dcb… drh 987 return seen;
6436ecf… wyoung 988 }
6436ecf… wyoung 989
6436ecf… wyoung 990 /*
fbffbe1… wyoung 991 ** Append an HTML attribute value, rewriting root-relative URLs in src
fbffbe1… wyoung 992 ** and srcset the same way (%R prefix).
fbffbe1… wyoung 993 */
fbffbe1… wyoung 994 static void appendRewrittenAttrValue(Blob *pOut, int iACode, const char *zVal){
fbffbe1… wyoung 995 if( iACode==ATTR_SRC && zVal[0]=='/' ){
fbffbe1… wyoung 996 blob_appendf(pOut, "=\"%R%s\"", zVal);
fbffbe1… wyoung 997 return;
fbffbe1… wyoung 998 }
fbffbe1… wyoung 999 if( iACode==ATTR_SRCSET ){
fbffbe1… wyoung 1000 const char *z = zVal;
fbffbe1… wyoung 1001 int nCand = 0;
fbffbe1… wyoung 1002 blob_append_char(pOut, '=');
fbffbe1… wyoung 1003 blob_append_char(pOut, '"');
fbffbe1… wyoung 1004 while( z[0] ){
fbffbe1… wyoung 1005 const char *zComma = strchr(z, ',');
fbffbe1… wyoung 1006 int len = zComma ? (int)(zComma - z) : (int)strlen(z);
fbffbe1… wyoung 1007 const char *zCand = z;
fbffbe1… wyoung 1008 int urlStart = 0;
fbffbe1… wyoung 1009 int urlLen;
fbffbe1… wyoung 1010 if( len>0 ){
fbffbe1… wyoung 1011 while( urlStart<len && fossil_isspace(zCand[urlStart]) ) urlStart++;
fbffbe1… wyoung 1012 urlLen = urlStart;
fbffbe1… wyoung 1013 while( urlLen<len && !fossil_isspace(zCand[urlLen]) ) urlLen++;
fbffbe1… wyoung 1014 urlLen -= urlStart;
fbffbe1… wyoung 1015 if( nCand++ ) blob_append_char(pOut, ',');
fbffbe1… wyoung 1016 if( urlStart>0 ){
fbffbe1… wyoung 1017 blob_append(pOut, zCand, urlStart);
fbffbe1… wyoung 1018 }
fbffbe1… wyoung 1019 if( urlLen>0 && zCand[urlStart]=='/' ){
fbffbe1… wyoung 1020 blob_appendf(pOut, "%R%.*s", urlLen, zCand+urlStart);
fbffbe1… wyoung 1021 }else if( urlLen>0 ){
fbffbe1… wyoung 1022 blob_appendf(pOut, "%.*s", urlLen, zCand+urlStart);
fbffbe1… wyoung 1023 }
fbffbe1… wyoung 1024 if( urlStart+urlLen < len ){
fbffbe1… wyoung 1025 blob_appendf(pOut, "%.*s", len-urlStart-urlLen, zCand+urlStart+urlLen);
fbffbe1… wyoung 1026 }
fbffbe1… wyoung 1027 }
fbffbe1… wyoung 1028 if( zComma==0 ) break;
fbffbe1… wyoung 1029 z = zComma + 1;
fbffbe1… wyoung 1030 }
fbffbe1… wyoung 1031 blob_append_char(pOut, '"');
fbffbe1… wyoung 1032 return;
fbffbe1… wyoung 1033 }
fbffbe1… wyoung 1034 blob_appendf(pOut, "=\"%s\"", zVal);
fbffbe1… wyoung 1035 }
fbffbe1… wyoung 1036
fbffbe1… wyoung 1037 /*
dbda8d6… drh 1038 ** Render markup on the given blob.
dbda8d6… drh 1039 */
dbda8d6… drh 1040 static void renderMarkup(Blob *pOut, ParsedMarkup *p){
dbda8d6… drh 1041 int i;
dbda8d6… drh 1042 if( p->endTag ){
dbda8d6… drh 1043 blob_appendf(pOut, "</%s>", aMarkup[p->iCode].zName);
dbda8d6… drh 1044 }else{
dbda8d6… drh 1045 blob_appendf(pOut, "<%s", aMarkup[p->iCode].zName);
dbda8d6… drh 1046 for(i=0; i<p->nAttr; i++){
e01aa8c… drh 1047 blob_appendf(pOut, " %s", aAttribute[p->aAttr[i].iACode].zName);
dbda8d6… drh 1048 if( p->aAttr[i].zValue ){
fbffbe1… wyoung 1049 appendRewrittenAttrValue(pOut, p->aAttr[i].iACode, p->aAttr[i].zValue);
dbda8d6… drh 1050 }
dbda8d6… drh 1051 }
3243e63… drh 1052 if (p->iType & MUTYPE_SINGLE){
48c47e1… drh 1053 blob_append_string(pOut, " /");
3243e63… drh 1054 }
48c47e1… drh 1055 blob_append_char(pOut, '>');
dbda8d6… drh 1056 }
dbda8d6… drh 1057 }
dbda8d6… drh 1058
dbda8d6… drh 1059 /*
dbda8d6… drh 1060 ** When the markup was parsed, some "\000" may have been inserted.
dbda8d6… drh 1061 ** This routine restores to those "\000" values back to their
dbda8d6… drh 1062 ** original content.
dbda8d6… drh 1063 */
dbda8d6… drh 1064 static void unparseMarkup(ParsedMarkup *p){
dbda8d6… drh 1065 int i, n;
dbda8d6… drh 1066 for(i=0; i<p->nAttr; i++){
dbda8d6… drh 1067 char *z = p->aAttr[i].zValue;
dbda8d6… drh 1068 if( z==0 ) continue;
d8586b5… drh 1069 if( p->aAttr[i].cTerm ){
d8586b5… drh 1070 n = strlen(z);
d8586b5… drh 1071 z[n] = p->aAttr[i].cTerm;
d8586b5… drh 1072 }
18c310a… drh 1073 }
18c310a… drh 1074 }
18c310a… drh 1075
18c310a… drh 1076 /*
18c310a… drh 1077 ** Return the value of attribute attrId. Return NULL if there is no
18c310a… drh 1078 ** ID attribute.
18c310a… drh 1079 */
18c310a… drh 1080 static const char *attributeValue(ParsedMarkup *p, int attrId){
18c310a… drh 1081 int i;
18c310a… drh 1082 for(i=0; i<p->nAttr; i++){
18c310a… drh 1083 if( p->aAttr[i].iACode==attrId ){
18c310a… drh 1084 return p->aAttr[i].zValue;
18c310a… drh 1085 }
dbda8d6… drh 1086 }
18c310a… drh 1087 return 0;
e01aa8c… drh 1088 }
e01aa8c… drh 1089
e01aa8c… drh 1090 /*
e01aa8c… drh 1091 ** Return the ID attribute for markup. Return NULL if there is no
e01aa8c… drh 1092 ** ID attribute.
e01aa8c… drh 1093 */
e01aa8c… drh 1094 static const char *markupId(ParsedMarkup *p){
18c310a… drh 1095 return attributeValue(p, ATTR_ID);
18c310a… drh 1096 }
18c310a… drh 1097
18c310a… drh 1098 /*
18c310a… drh 1099 ** Check markup pMarkup to see if it is a hyperlink with class "button"
18c310a… drh 1100 ** that is follows by simple text and an </a> only. Example:
18c310a… drh 1101 **
18c310a… drh 1102 ** <a class="button" href="../index.wiki">Index</a>
18c310a… drh 1103 **
18c310a… drh 1104 ** If the markup matches this pattern, and if the WIKI_BUTTONS flag was
18c310a… drh 1105 ** passed to wiki_convert(), then transform this link into a submenu
18c310a… drh 1106 ** button, skip the text, and set *pN equal to the total length of the
70b4f10… jan.nijtmans 1107 ** text through the end of </a> and return true. If the markup does
18c310a… drh 1108 ** not match or if WIKI_BUTTONS is not set, then make no changes to *pN
18c310a… drh 1109 ** and return false.
18c310a… drh 1110 */
18c310a… drh 1111 static int isButtonHyperlink(
18c310a… drh 1112 Renderer *p, /* Renderer state */
18c310a… drh 1113 ParsedMarkup *pMarkup, /* Potential button markup */
18c310a… drh 1114 const char *z, /* Complete text of Wiki */
18c310a… drh 1115 int *pN /* Characters of z[] consumed */
18c310a… drh 1116 ){
18c310a… drh 1117 const char *zClass;
18c310a… drh 1118 const char *zHref;
18c310a… drh 1119 char *zTag;
18c310a… drh 1120 int i, j;
18c310a… drh 1121 if( (p->state & WIKI_BUTTONS)==0 ) return 0;
18c310a… drh 1122 zClass = attributeValue(pMarkup, ATTR_CLASS);
18c310a… drh 1123 if( zClass==0 ) return 0;
18c310a… drh 1124 if( fossil_strcmp(zClass, "button")!=0 ) return 0;
18c310a… drh 1125 zHref = attributeValue(pMarkup, ATTR_HREF);
18c310a… drh 1126 if( zHref==0 ) return 0;
18c310a… drh 1127 i = *pN;
18c310a… drh 1128 while( z[i] && z[i]!='<' ){ i++; }
18c310a… drh 1129 if( fossil_strnicmp(&z[i], "</a>",4)!=0 ) return 0;
18c310a… drh 1130 for(j=*pN; fossil_isspace(z[j]); j++){}
18c310a… drh 1131 zTag = mprintf("%.*s", i-j, &z[j]);
18c310a… drh 1132 j = (int)strlen(zTag);
18c310a… drh 1133 while( j>0 && fossil_isspace(zTag[j-1]) ){ j--; }
18c310a… drh 1134 if( j==0 ) return 0;
187424e… andygoth 1135 style_submenu_element(zTag, "%s", zHref);
18c310a… drh 1136 *pN = i+4;
18c310a… drh 1137 return 1;
d0305b3… aku 1138 }
dbda8d6… drh 1139
dbda8d6… drh 1140 /*
dbda8d6… drh 1141 ** Pop a single element off of the stack. As the element is popped,
e01aa8c… drh 1142 ** output its end tag if it is not a </div> tag.
dbda8d6… drh 1143 */
dbda8d6… drh 1144 static void popStack(Renderer *p){
dbda8d6… drh 1145 if( p->nStack ){
e01aa8c… drh 1146 int iCode;
dbda8d6… drh 1147 p->nStack--;
e01aa8c… drh 1148 iCode = p->aStack[p->nStack].iCode;
a6e4dcb… drh 1149 if( (iCode!=MARKUP_DIV || p->aStack[p->nStack].zId==0) && p->pOut ){
e01aa8c… drh 1150 blob_appendf(p->pOut, "</%s>", aMarkup[iCode].zName);
e01aa8c… drh 1151 }
dbda8d6… drh 1152 }
dbda8d6… drh 1153 }
dbda8d6… drh 1154
dbda8d6… drh 1155 /*
dbda8d6… drh 1156 ** Push a new markup value onto the stack. Enlarge the stack
dbda8d6… drh 1157 ** if necessary.
dbda8d6… drh 1158 */
e01aa8c… drh 1159 static void pushStackWithId(Renderer *p, int elem, const char *zId, int w){
dbda8d6… drh 1160 if( p->nStack>=p->nAlloc ){
dbda8d6… drh 1161 p->nAlloc = p->nAlloc*2 + 100;
8f41b2f… drh 1162 p->aStack = fossil_realloc(p->aStack, p->nAlloc*sizeof(p->aStack[0]));
dbda8d6… drh 1163 }
e01aa8c… drh 1164 p->aStack[p->nStack].iCode = elem;
e01aa8c… drh 1165 p->aStack[p->nStack].zId = zId;
e01aa8c… drh 1166 p->aStack[p->nStack].allowWiki = w;
e01aa8c… drh 1167 p->nStack++;
e01aa8c… drh 1168 }
e01aa8c… drh 1169 static void pushStack(Renderer *p, int elem){
e01aa8c… drh 1170 pushStackWithId(p, elem, 0, 0);
dbda8d6… drh 1171 }
dbda8d6… drh 1172
dbda8d6… drh 1173 /*
dbda8d6… drh 1174 ** Pop the stack until the top-most iTag element is removed.
dbda8d6… drh 1175 ** If there is no iTag element on the stack, this routine
dbda8d6… drh 1176 ** is a no-op.
dbda8d6… drh 1177 */
dbda8d6… drh 1178 static void popStackToTag(Renderer *p, int iTag){
dbda8d6… drh 1179 int i;
e01aa8c… drh 1180 for(i=p->nStack-1; i>=0; i--){
e01aa8c… drh 1181 if( p->aStack[i].iCode!=iTag ) continue;
e01aa8c… drh 1182 if( p->aStack[i].zId ) continue;
e01aa8c… drh 1183 break;
e01aa8c… drh 1184 }
dbda8d6… drh 1185 if( i<0 ) return;
dbda8d6… drh 1186 while( p->nStack>i ){
dbda8d6… drh 1187 popStack(p);
dbda8d6… drh 1188 }
e01aa8c… drh 1189 }
e01aa8c… drh 1190
e01aa8c… drh 1191 /*
e01aa8c… drh 1192 ** Attempt to find a find a tag of type iTag with id zId. Return -1
b9d3b42… drh 1193 ** if not found. If found, return its stack level.
e01aa8c… drh 1194 */
e01aa8c… drh 1195 static int findTagWithId(Renderer *p, int iTag, const char *zId){
e01aa8c… drh 1196 int i;
e01aa8c… drh 1197 assert( zId!=0 );
e01aa8c… drh 1198 for(i=p->nStack-1; i>=0; i--){
e01aa8c… drh 1199 if( p->aStack[i].iCode!=iTag ) continue;
e01aa8c… drh 1200 if( p->aStack[i].zId==0 ) continue;
32ad9a1… drh 1201 if( fossil_strcmp(zId, p->aStack[i].zId)!=0 ) continue;
e01aa8c… drh 1202 break;
e01aa8c… drh 1203 }
e01aa8c… drh 1204 return i;
d0305b3… aku 1205 }
d0305b3… aku 1206
d0305b3… aku 1207 /*
dbda8d6… drh 1208 ** Pop the stack until the top-most element of the stack
dbda8d6… drh 1209 ** is an element that matches the type in iMask. Return
d0305b3… aku 1210 ** code of the markup element that is on left on top of the stack.
d0305b3… aku 1211 ** If the stack does not have an element
dbda8d6… drh 1212 ** that matches iMask, then leave the stack unchanged and
d0305b3… aku 1213 ** return false (MARKUP_INVALID).
dbda8d6… drh 1214 */
dbda8d6… drh 1215 static int backupToType(Renderer *p, int iMask){
dbda8d6… drh 1216 int i;
e01aa8c… drh 1217 for(i=p->nStack-1; i>=0; i--){
e01aa8c… drh 1218 if( aMarkup[p->aStack[i].iCode].iType & iMask ) break;
e01aa8c… drh 1219 }
dbda8d6… drh 1220 if( i<0 ) return 0;
dbda8d6… drh 1221 i++;
dbda8d6… drh 1222 while( p->nStack>i ){
dbda8d6… drh 1223 popStack(p);
dbda8d6… drh 1224 }
e01aa8c… drh 1225 return p->aStack[i-1].iCode;
d0305b3… aku 1226 }
d0305b3… aku 1227
d0305b3… aku 1228 /*
d0305b3… aku 1229 ** Begin a new paragraph if that something that is needed.
d0305b3… aku 1230 */
d0305b3… aku 1231 static void startAutoParagraph(Renderer *p){
3243e63… drh 1232 if( p->wantAutoParagraph==0 ) return;
4615bc8… drh 1233 if( p->state & WIKI_LINKSONLY ) return;
3243e63… drh 1234 if( p->wikiList==MARKUP_OL || p->wikiList==MARKUP_UL ) return;
48c47e1… drh 1235 blob_append_string(p->pOut, "<p>");
d0305b3… aku 1236 p->wantAutoParagraph = 0;
d0305b3… aku 1237 p->inAutoParagraph = 1;
d0305b3… aku 1238 }
d0305b3… aku 1239
d0305b3… aku 1240 /*
d0305b3… aku 1241 ** End a paragraph if we are in one.
d0305b3… aku 1242 */
d0305b3… aku 1243 static void endAutoParagraph(Renderer *p){
083cad8… stephan 1244 if( p->inAutoParagraph ){
d0305b3… aku 1245 p->inAutoParagraph = 0;
d0305b3… aku 1246 }
d0305b3… aku 1247 }
d0305b3… aku 1248
d0305b3… aku 1249 /*
d0305b3… aku 1250 ** If the input string corresponds to an existing baseline,
d0305b3… aku 1251 ** return true.
d0305b3… aku 1252 */
e0757d2… drh 1253 static int is_valid_hname(const char *z){
d0305b3… aku 1254 int n = strlen(z);
fd9b7bd… drh 1255 if( n<4 || n>HNAME_MAX ) return 0;
d0305b3… aku 1256 if( !validate16(z, n) ) return 0;
dbda8d6… drh 1257 return 1;
a99e9c9… drh 1258 }
a99e9c9… drh 1259
a99e9c9… drh 1260 /*
e0757d2… drh 1261 ** Return TRUE if a hash name corresponds to an artifact in this
a99e9c9… drh 1262 ** repository.
a99e9c9… drh 1263 */
a99e9c9… drh 1264 static int in_this_repo(const char *zUuid){
a99e9c9… drh 1265 static Stmt q;
a99e9c9… drh 1266 int rc;
09681e1… drh 1267 int n;
e0757d2… drh 1268 char zU2[HNAME_MAX+1];
70b4f10… jan.nijtmans 1269 db_static_prepare(&q,
09681e1… drh 1270 "SELECT 1 FROM blob WHERE uuid>=:u AND uuid<:u2"
a99e9c9… drh 1271 );
a99e9c9… drh 1272 db_bind_text(&q, ":u", zUuid);
09681e1… drh 1273 n = (int)strlen(zUuid);
53db40e… drh 1274 if( n>=(int)sizeof(zU2) ) n = sizeof(zU2)-1;
09681e1… drh 1275 memcpy(zU2, zUuid, n);
09681e1… drh 1276 zU2[n-1]++;
09681e1… drh 1277 zU2[n] = 0;
09681e1… drh 1278 db_bind_text(&q, ":u2", zU2);
a99e9c9… drh 1279 rc = db_step(&q);
a99e9c9… drh 1280 db_reset(&q);
a99e9c9… drh 1281 return rc==SQLITE_ROW;
f0c8693… drh 1282 }
f0c8693… drh 1283
f0c8693… drh 1284 /*
ac3f1f2… drh 1285 ** zTarget is guaranteed to be a UUID. It might be the UUID of a ticket.
f0c8693… drh 1286 ** If it is, store in *pClosed a true or false depending on whether or not
f0c8693… drh 1287 ** the ticket is closed and return true. If zTarget
ac3f1f2… drh 1288 ** is not the UUID of a ticket, return false.
ac3f1f2… drh 1289 */
d80adfe… drh 1290 int is_ticket(
ac3f1f2… drh 1291 const char *zTarget, /* Ticket UUID */
ac3f1f2… drh 1292 int *pClosed /* True if the ticket is closed */
ac3f1f2… drh 1293 ){
ac3f1f2… drh 1294 static Stmt q;
ac3f1f2… drh 1295 int n;
ac3f1f2… drh 1296 int rc;
323299c… drh 1297 char zLower[HNAME_MAX+1];
323299c… drh 1298 char zUpper[HNAME_MAX+1];
ac3f1f2… drh 1299 n = strlen(zTarget);
ac3f1f2… drh 1300 memcpy(zLower, zTarget, n+1);
ac3f1f2… drh 1301 canonical16(zLower, n+1);
ac3f1f2… drh 1302 memcpy(zUpper, zLower, n+1);
ac3f1f2… drh 1303 zUpper[n-1]++;
70f3092… drh 1304 if( !db_static_stmt_is_init(&q) ){
7949d94… drh 1305 char *zClosedExpr = db_get("ticket-closed-expr", "status='Closed'");
b9d3b42… drh 1306 db_static_prepare(&q,
7949d94… drh 1307 "SELECT %z FROM ticket "
ac3f1f2… drh 1308 " WHERE tkt_uuid>=:lwr AND tkt_uuid<:upr",
49b0ff1… drh 1309 zClosedExpr /*safe-for-%s*/
ac3f1f2… drh 1310 );
ac3f1f2… drh 1311 }
ac3f1f2… drh 1312 db_bind_text(&q, ":lwr", zLower);
ac3f1f2… drh 1313 db_bind_text(&q, ":upr", zUpper);
ac3f1f2… drh 1314 if( db_step(&q)==SQLITE_ROW ){
ac3f1f2… drh 1315 rc = 1;
f0c8693… drh 1316 *pClosed = db_column_int(&q, 0);
ac3f1f2… drh 1317 }else{
ac3f1f2… drh 1318 rc = 0;
ac3f1f2… drh 1319 }
ac3f1f2… drh 1320 db_reset(&q);
ac3f1f2… drh 1321 return rc;
ac3f1f2… drh 1322 }
ac3f1f2… drh 1323
ac3f1f2… drh 1324 /*
90676f4… drh 1325 ** Return a pointer to the name part of zTarget (skipping the "wiki:" prefix
90676f4… drh 1326 ** if there is one) if zTarget is a valid wiki page name. Return NULL if
90676f4… drh 1327 ** zTarget names a page that does not exist.
90676f4… drh 1328 */
774fb77… drh 1329 static const char *validWikiPageName(int mFlags, const char *zTarget){
90676f4… drh 1330 if( strncmp(zTarget, "wiki:", 5)==0
90676f4… drh 1331 && wiki_name_is_wellformed((const unsigned char*)zTarget) ){
90676f4… drh 1332 return zTarget+5;
90676f4… drh 1333 }
90676f4… drh 1334 if( strcmp(zTarget, "Sandbox")==0 ) return zTarget;
90676f4… drh 1335 if( wiki_name_is_wellformed((const unsigned char *)zTarget)
774fb77… drh 1336 && ((mFlags & WIKI_NOBADLINKS)==0 ||
425df28… drh 1337 db_exists("SELECT 1 FROM tag WHERE tagname GLOB 'wiki-%q'"
425df28… drh 1338 " AND (SELECT value FROM tagxref WHERE tagid=tag.tagid"
425df28… drh 1339 " ORDER BY mtime DESC LIMIT 1) > 0", zTarget))
26ac392… drh 1340 ){
90676f4… drh 1341 return zTarget;
90676f4… drh 1342 }
90676f4… drh 1343 return 0;
606421e… drh 1344 }
606421e… drh 1345
606421e… drh 1346 static const char *wikiOverrideHash = 0;
606421e… drh 1347
606421e… drh 1348 /*
606421e… drh 1349 ** Fossil-wiki hyperlinks to wiki pages should be overridden to the
606421e… drh 1350 ** hash value supplied. If the value is NULL, then override is cancelled
606421e… drh 1351 ** and all overwrites operate normally.
606421e… drh 1352 */
606421e… drh 1353 void wiki_hyperlink_override(const char *zUuid){
606421e… drh 1354 wikiOverrideHash = zUuid;
606421e… drh 1355 }
606421e… drh 1356
606421e… drh 1357
606421e… drh 1358 /*
606421e… drh 1359 ** If links to wiki page zTarget should be redirected to some historical
606421e… drh 1360 ** version of that page, then return the hash of the historical version.
606421e… drh 1361 ** If no override is required, return NULL.
606421e… drh 1362 */
606421e… drh 1363 static const char *wiki_is_overridden(const char *zTarget){
606421e… drh 1364 if( wikiOverrideHash==0 ) return 0;
606421e… drh 1365 /* The override should only happen if the override version is not the
606421e… drh 1366 ** latest version of the wiki page. */
606421e… drh 1367 if( !db_exists(
606421e… drh 1368 "SELECT 1 FROM tag, blob, tagxref AS xA, tagxref AS xB "
606421e… drh 1369 " WHERE tag.tagname GLOB 'wiki-%q*'"
606421e… drh 1370 " AND blob.uuid GLOB '%q'"
606421e… drh 1371 " AND xA.tagid=tag.tagid AND xA.rid=blob.rid"
606421e… drh 1372 " AND xB.tagid=tag.tagid AND xB.mtime>xA.mtime",
606421e… drh 1373 zTarget, wikiOverrideHash
606421e… drh 1374 ) ){
606421e… drh 1375 return 0;
606421e… drh 1376 }
606421e… drh 1377 return wikiOverrideHash;
90676f4… drh 1378 }
90676f4… drh 1379
90676f4… drh 1380 /*
ac3f1f2… drh 1381 ** Resolve a hyperlink. The zTarget argument is the content of the [...]
db0c512… drh 1382 ** in the wiki. Append to the output string whatever text is appropriate
f0c8693… drh 1383 ** for opening the hyperlink. Write into zClose[0...nClose-1] text that will
f0c8693… drh 1384 ** close the markup.
90676f4… drh 1385 **
90676f4… drh 1386 ** If this routine determines that no hyperlink should be generated, then
90676f4… drh 1387 ** set zClose[0] to 0.
ac3f1f2… drh 1388 **
ac3f1f2… drh 1389 ** Actually, this routine might or might not append the hyperlink, depending
ac3f1f2… drh 1390 ** on current rendering rules: specifically does the current user have
f0c8693… drh 1391 ** "History" permission.
3a4c7f6… drh 1392 **
09908ab… drh 1393 ** [http://fossil-scm.org/]
09908ab… drh 1394 ** [https://fossil-scm.org/]
09908ab… drh 1395 ** [ftp://fossil-scm.org/]
3a4c7f6… drh 1396 ** [mailto:[email protected]]
3a4c7f6… drh 1397 **
3b10e64… drh 1398 ** [/path] -> Refers to the root of the Fossil hierarchy, not
3b10e64… drh 1399 ** the root of the URI domain
3a4c7f6… drh 1400 **
3a4c7f6… drh 1401 ** [./relpath]
3b10e64… drh 1402 ** [../relpath]
3b10e64… drh 1403 **
3b10e64… drh 1404 ** [#fragment]
3b10e64… drh 1405 **
3b10e64… drh 1406 ** [0123456789abcdef]
d6b2c38… drh 1407 **
d6b2c38… drh 1408 ** [WikiPageName]
d6b2c38… drh 1409 ** [wiki:WikiPageName]
d6b2c38… drh 1410 **
b9d3b42… drh 1411 ** [2010-02-27 07:13]
f4dc114… drh 1412 **
f4dc114… drh 1413 ** [InterMap:Link] -> Interwiki link
53b080b… drh 1414 **
53b080b… drh 1415 ** The return value is a mask of RENDER_* values indicating what happened.
53b080b… drh 1416 ** Probably the return value is 0 on success and RENDER_BADTARGET or
53b080b… drh 1417 ** RENDER_BADLINK if there are problems.
dbda8d6… drh 1418 */
53b080b… drh 1419 int wiki_resolve_hyperlink(
774fb77… drh 1420 Blob *pOut, /* Write the HTML output here */
774fb77… drh 1421 int mFlags, /* Rendering option flags */
db0c512… drh 1422 const char *zTarget, /* Hyperlink target; text within [...] */
f0c8693… drh 1423 char *zClose, /* Write hyperlink closing text here */
b7abba6… drh 1424 int nClose, /* Bytes available in zClose[] */
774fb77… drh 1425 const char *zOrig, /* Complete document text */
774fb77… drh 1426 const char *zTitle /* Title of the link */
ac3f1f2… drh 1427 ){
f0c8693… drh 1428 const char *zTerm = "</a>";
90676f4… drh 1429 const char *z;
774fb77… drh 1430 char *zExtra = 0;
774fb77… drh 1431 const char *zExtraNS = 0;
f4dc114… drh 1432 char *zRemote = 0;
53b080b… drh 1433 int rc = 0;
90676f4… drh 1434
774fb77… drh 1435 if( zTitle ){
774fb77… drh 1436 zExtra = mprintf(" title='%h'", zTitle);
822653c… drh 1437 zExtraNS = zExtra+1;
822653c… drh 1438 }else if( mFlags & WIKI_TARGET_BLANK ){
822653c… drh 1439 zExtra = mprintf(" target='_blank'");
774fb77… drh 1440 zExtraNS = zExtra+1;
774fb77… drh 1441 }
77dc754… drh 1442 assert( nClose>=20 );
b9d3b42… drh 1443 if( strncmp(zTarget, "http:", 5)==0
d0305b3… aku 1444 || strncmp(zTarget, "https:", 6)==0
b9d3b42… drh 1445 || strncmp(zTarget, "ftp:", 4)==0
d0305b3… aku 1446 || strncmp(zTarget, "mailto:", 7)==0
d0305b3… aku 1447 ){
774fb77… drh 1448 blob_appendf(pOut, "<a href=\"%s\"%s>", zTarget, zExtra);
d0305b3… aku 1449 }else if( zTarget[0]=='/' ){
774fb77… drh 1450 blob_appendf(pOut, "<a href=\"%R%h\"%s>", zTarget, zExtra);
4f8c897… drh 1451 }else if( zTarget[0]=='.'
4f8c897… drh 1452 && (zTarget[1]=='/' || (zTarget[1]=='.' && zTarget[2]=='/'))
774fb77… drh 1453 && (mFlags & WIKI_LINKSONLY)==0 ){
774fb77… drh 1454 blob_appendf(pOut, "<a href=\"%h\"%s>", zTarget, zExtra);
4f8c897… drh 1455 }else if( zTarget[0]=='#' ){
774fb77… drh 1456 blob_appendf(pOut, "<a href=\"%h\"%s>", zTarget, zExtra);
e0757d2… drh 1457 }else if( is_valid_hname(zTarget) ){
e0131b1… drh 1458 int isClosed = 0;
bd67c3c… drh 1459 const char *zLB = (mFlags & WIKI_NOBRACKET)==0 ? "[" : "";
323299c… drh 1460 if( strlen(zTarget)<=HNAME_MAX && is_ticket(zTarget, &isClosed) ){
ac3f1f2… drh 1461 /* Special display processing for tickets. Display the hyperlink
f0c8693… drh 1462 ** as crossed out if the ticket is closed.
ac3f1f2… drh 1463 */
ac3f1f2… drh 1464 if( isClosed ){
433cde1… drh 1465 if( g.perm.Hyperlink ){
774fb77… drh 1466 blob_appendf(pOut,
bd67c3c… drh 1467 "%z<span class=\"wikiTagCancelled\">%s",
bd67c3c… drh 1468 xhref(zExtraNS,"%R/info/%s",zTarget), zLB
774fb77… drh 1469 );
774fb77… drh 1470 zTerm = "]</span></a>";
774fb77… drh 1471 }else{
bd67c3c… drh 1472 blob_appendf(pOut,"<span class=\"wikiTagCancelled\">%s", zLB);
774fb77… drh 1473 zTerm = "]</span>";
774fb77… drh 1474 }
774fb77… drh 1475 }else{
774fb77… drh 1476 if( g.perm.Hyperlink ){
bd67c3c… drh 1477 blob_appendf(pOut,"%z%s", xhref(zExtraNS,"%R/info/%s", zTarget),zLB);
774fb77… drh 1478 zTerm = "]</a>";
774fb77… drh 1479 }else{
bd67c3c… drh 1480 blob_appendf(pOut, "%s", zLB);
774fb77… drh 1481 zTerm = "]";
774fb77… drh 1482 }
774fb77… drh 1483 }
774fb77… drh 1484 }else if( !in_this_repo(zTarget) ){
774fb77… drh 1485 if( (mFlags & (WIKI_LINKSONLY|WIKI_NOBADLINKS))!=0 ){
774fb77… drh 1486 zTerm = "";
f0d9fe2… drh 1487 }else if( (mFlags & WIKI_MARK)!=0 ){
f0d9fe2… drh 1488 blob_appendf(pOut, "<mark>%s", zLB);
f0d9fe2… drh 1489 zTerm = "]</mark>";
f0d9fe2… drh 1490 rc |= RENDER_MARK;
774fb77… drh 1491 }else{
bd67c3c… drh 1492 blob_appendf(pOut, "<span class=\"brokenlink\">%s", zLB);
774fb77… drh 1493 zTerm = "]</span>";
774fb77… drh 1494 }
53b080b… drh 1495 rc |= RENDER_BADTARGET;
3e87f78… drh 1496 }else if( g.perm.Hyperlink || (mFlags & WIKI_ADMIN)!=0 ){
bd67c3c… drh 1497 blob_appendf(pOut, "%z%s",xhref(zExtraNS, "%R/info/%s", zTarget), zLB);
774fb77… drh 1498 zTerm = "]</a>";
774fb77… drh 1499 }else{
774fb77… drh 1500 zTerm = "";
774fb77… drh 1501 }
bd67c3c… drh 1502 if( zTerm[0]==']' && (mFlags & WIKI_NOBRACKET)!=0 ) zTerm++;
f4dc114… drh 1503 }else if( (zRemote = interwiki_url(zTarget))!=0 ){
f4dc114… drh 1504 blob_appendf(pOut, "<a href=\"%z\"%s>", zRemote, zExtra);
f4dc114… drh 1505 zTerm = "</a>";
774fb77… drh 1506 }else if( (z = validWikiPageName(mFlags, zTarget))!=0 ){
3b10e64… drh 1507 /* The link is to a valid wiki page name */
774fb77… drh 1508 const char *zOverride = wiki_is_overridden(zTarget);
774fb77… drh 1509 if( zOverride ){
774fb77… drh 1510 blob_appendf(pOut, "<a href=\"%R/info/%S\"%s>", zOverride, zExtra);
774fb77… drh 1511 }else{
774fb77… drh 1512 blob_appendf(pOut, "<a href=\"%R/wiki?name=%T\"%s>", z, zExtra);
774fb77… drh 1513 }
3b10e64… drh 1514 }else if( strlen(zTarget)>=10 && fossil_isdigit(zTarget[0]) && zTarget[4]=='-'
3b10e64… drh 1515 && db_int(0, "SELECT datetime(%Q) NOT NULL", zTarget) ){
53b080b… drh 1516 /* Dates or date-and-times in ISO8601 resolve to a link to the
3b10e64… drh 1517 ** timeline for that date */
3b10e64… drh 1518 blob_appendf(pOut, "<a href=\"%R/timeline?c=%T\"%s>", zTarget, zExtra);
3b10e64… drh 1519 }else if( mFlags & WIKI_MARKDOWNLINKS ){
3b10e64… drh 1520 /* If none of the above, and if rendering links for markdown, then
3b10e64… drh 1521 ** create a link to the literal text of the target */
3b10e64… drh 1522 blob_appendf(pOut, "<a href=\"%h\"%s>", zTarget, zExtra);
f0d9fe2… drh 1523 }else if( mFlags & WIKI_MARK ){
f0d9fe2… drh 1524 blob_appendf(pOut, "<mark>[");
f0d9fe2… drh 1525 zTerm = "]</mark>";
f0d9fe2… drh 1526 rc |= RENDER_BADTARGET|RENDER_MARK;
3b10e64… drh 1527 }else if( zOrig && zTarget>=&zOrig[2]
3b10e64… drh 1528 && zTarget[-1]=='[' && !fossil_isspace(zTarget[-2]) ){
3b10e64… drh 1529 /* If the hyperlink markup is not preceded by whitespace, then it
3b10e64… drh 1530 ** is probably a C-language subscript or similar, not really a
3b10e64… drh 1531 ** hyperlink. Just ignore it. */
774fb77… drh 1532 zTerm = "";
774fb77… drh 1533 }else if( (mFlags & (WIKI_NOBADLINKS|WIKI_LINKSONLY))!=0 ){
3b10e64… drh 1534 /* Also ignore the link if various flags are set */
774fb77… drh 1535 zTerm = "";
53b080b… drh 1536 rc |= RENDER_BADTARGET;
774fb77… drh 1537 }else{
774fb77… drh 1538 blob_appendf(pOut, "<span class=\"brokenlink\">[%h]", zTarget);
774fb77… drh 1539 zTerm = "</span>";
53b080b… drh 1540 rc |= RENDER_BADTARGET;
774fb77… drh 1541 }
774fb77… drh 1542 if( zExtra ) fossil_free(zExtra);
53db40e… drh 1543 assert( (int)strlen(zTerm)<nClose );
774fb77… drh 1544 sqlite3_snprintf(nClose, zClose, "%s", zTerm);
53b080b… drh 1545 return rc;
3e87f78… drh 1546 }
3e87f78… drh 1547
3e87f78… drh 1548 /*
3e87f78… drh 1549 ** Check zTarget to see if it looks like a valid hyperlink target.
3e87f78… drh 1550 ** Return true if it does seem valid and false if not.
3e87f78… drh 1551 */
3e87f78… drh 1552 int wiki_valid_link_target(char *zTarget){
3e87f78… drh 1553 char zClose[30];
3e87f78… drh 1554 Blob notUsed;
3e87f78… drh 1555 blob_init(&notUsed, 0, 0);
3e87f78… drh 1556 wiki_resolve_hyperlink(&notUsed, WIKI_NOBADLINKS|WIKI_ADMIN,
3e87f78… drh 1557 zTarget, zClose, sizeof(zClose)-1, 0, 0);
3e87f78… drh 1558 blob_reset(&notUsed);
3e87f78… drh 1559 return zClose[0]!=0;
dbda8d6… drh 1560 }
dbda8d6… drh 1561
dbda8d6… drh 1562 /*
dbda8d6… drh 1563 ** Check to see if the given parsed markup is the correct
dbda8d6… drh 1564 ** </verbatim> tag.
dbda8d6… drh 1565 */
dbda8d6… drh 1566 static int endVerbatim(Renderer *p, ParsedMarkup *pMarkup){
dbda8d6… drh 1567 char *z;
dbda8d6… drh 1568 assert( p->inVerbatim );
dbda8d6… drh 1569 if( pMarkup->iCode!=MARKUP_VERBATIM ) return 0;
dbda8d6… drh 1570 if( !pMarkup->endTag ) return 0;
dbda8d6… drh 1571 if( p->zVerbatimId==0 ) return 1;
dbda8d6… drh 1572 if( pMarkup->nAttr!=1 ) return 0;
dbda8d6… drh 1573 z = pMarkup->aAttr[0].zValue;
32ad9a1… drh 1574 return fossil_strcmp(z, p->zVerbatimId)==0;
1fc2df9… drh 1575 }
1fc2df9… drh 1576
1fc2df9… drh 1577 /*
1fc2df9… drh 1578 ** z[] points to the text that immediately follows markup of the form:
1fc2df9… drh 1579 **
1fc2df9… drh 1580 ** <verbatim type='pikchr ...'>
1fc2df9… drh 1581 **
1fc2df9… drh 1582 ** zClass is the argument to "type". This routine will process the
1fc2df9… drh 1583 ** Pikchr text through the next matching </verbatim> (or until end-of-file)
1fc2df9… drh 1584 ** and append the resulting SVG output onto p. It then returns the
1fc2df9… drh 1585 ** number of bytes of text processed, including the closing </verbatim>.
1fc2df9… drh 1586 */
1fc2df9… drh 1587 static int wiki_process_pikchr(Renderer *p, char *z, const char *zClass){
1fc2df9… drh 1588 ParsedMarkup m; /* Parsed closing tag */
1fc2df9… drh 1589 int i = 0; /* For looping over z[] in search of </verbatim> */
1fc2df9… drh 1590 int iRet = 0; /* Value to return */
1fc2df9… drh 1591 int atEnd = 0; /* True if se have found the </verbatim> */
1fc2df9… drh 1592 int nMarkup = 0; /* Length of a markup we are checking */
1fc2df9… drh 1593
1fc2df9… drh 1594 /* Search for the closing </verbatim> tag */
1fc2df9… drh 1595 while( z[i]!=0 ){
1fc2df9… drh 1596 char *zEnd = strchr(z+i, '<');
1fc2df9… drh 1597 if( zEnd==0 ){
1fc2df9… drh 1598 i += (int)strlen(z+i);
1fc2df9… drh 1599 iRet = i;
1fc2df9… drh 1600 break;
1fc2df9… drh 1601 }
1fc2df9… drh 1602 nMarkup = html_tag_length(zEnd);
1fc2df9… drh 1603 if( nMarkup<11 || fossil_strnicmp(zEnd, "</verbatim", 10)!=0 ){
1fc2df9… drh 1604 i = (int)(zEnd - z) + 1;
1fc2df9… drh 1605 continue;
1fc2df9… drh 1606 }
1fc2df9… drh 1607 (void)parseMarkup(&m, z+i);
1fc2df9… drh 1608 atEnd = endVerbatim(p, &m);
1fc2df9… drh 1609 unparseMarkup(&m);
1fc2df9… drh 1610 if( atEnd ){
1fc2df9… drh 1611 iRet = i + nMarkup;
1fc2df9… drh 1612 break;
1fc2df9… drh 1613 }
1fc2df9… drh 1614 i++;
1fc2df9… drh 1615 }
1fc2df9… drh 1616
1fc2df9… drh 1617 /* The Pikchr source text should be i character in length and iRet is
1fc2df9… drh 1618 ** i plus the number of bytes in the </verbatim>. Generate the reply.
1fc2df9… drh 1619 */
1fc2df9… drh 1620 assert( strncmp(zClass,"pikchr",6)==0 );
1fc2df9… drh 1621 zClass += 6;
1fc2df9… drh 1622 while( fossil_isspace(zClass[0]) ) zClass++;
a0ef47e… drh 1623 blob_append(p->pOut, "<p>", 3);
1fc2df9… drh 1624 pikchr_to_html(p->pOut, z, i, zClass, (int)strlen(zClass));
a0ef47e… drh 1625 blob_append(p->pOut, "</p>\n", 5);
1fc2df9… drh 1626 return iRet;
dbda8d6… drh 1627 }
dbda8d6… drh 1628
dbda8d6… drh 1629 /*
dbda8d6… drh 1630 ** Return the MUTYPE for the top of the stack.
dbda8d6… drh 1631 */
dbda8d6… drh 1632 static int stackTopType(Renderer *p){
dbda8d6… drh 1633 if( p->nStack<=0 ) return 0;
e01aa8c… drh 1634 return aMarkup[p->aStack[p->nStack-1].iCode].iType;
dbda8d6… drh 1635 }
dbda8d6… drh 1636
dbda8d6… drh 1637 /*
dbda8d6… drh 1638 ** Convert the wiki in z[] into html in the renderer p. The
dbda8d6… drh 1639 ** renderer has already been initialized.
dbda8d6… drh 1640 **
dbda8d6… drh 1641 ** This routine will probably modify the content of z[].
dbda8d6… drh 1642 */
dbda8d6… drh 1643 static void wiki_render(Renderer *p, char *z){
dbda8d6… drh 1644 int tokenType;
dbda8d6… drh 1645 ParsedMarkup markup;
dbda8d6… drh 1646 int n;
4615bc8… drh 1647 int wikiHtmlOnly = (p->state & (WIKI_HTMLONLY | WIKI_LINKSONLY))!=0;
4615bc8… drh 1648 int linksOnly = (p->state & WIKI_LINKSONLY)!=0;
b7abba6… drh 1649 char *zOrig = z;
cb44f7d… drh 1650
cb44f7d… drh 1651 /* Make sure the attribute constants and names still align
cb44f7d… drh 1652 ** following changes in the attribute list. */
32ad9a1… drh 1653 assert( fossil_strcmp(aAttribute[ATTR_WIDTH].zName, "width")==0 );
dbda8d6… drh 1654
dbda8d6… drh 1655 while( z[0] ){
4615bc8… drh 1656 if( wikiHtmlOnly ){
cf3809c… jeremy_c 1657 n = nextRawToken(z, p, &tokenType);
cf3809c… jeremy_c 1658 }else{
cf3809c… jeremy_c 1659 n = nextWikiToken(z, p, &tokenType);
cf3809c… jeremy_c 1660 }
dbda8d6… drh 1661 p->state &= ~(AT_NEWLINE|AT_PARAGRAPH);
dbda8d6… drh 1662 switch( tokenType ){
dbda8d6… drh 1663 case TOKEN_PARAGRAPH: {
0432c38… drh 1664 if( p->wikiList ){
0432c38… drh 1665 popStackToTag(p, p->wikiList);
0432c38… drh 1666 p->wikiList = 0;
0432c38… drh 1667 }
0432c38… drh 1668 endAutoParagraph(p);
0432c38… drh 1669 blob_append_string(p->pOut, "\n\n");
0432c38… drh 1670 p->wantAutoParagraph = 1;
dbda8d6… drh 1671 p->state |= AT_PARAGRAPH|AT_NEWLINE;
dbda8d6… drh 1672 break;
dbda8d6… drh 1673 }
dbda8d6… drh 1674 case TOKEN_NEWLINE: {
8af0255… drh 1675 if( p->renderFlags & WIKI_NEWLINE ){
48c47e1… drh 1676 blob_append_string(p->pOut, "<br>\n");
8af0255… drh 1677 }else{
48c47e1… drh 1678 blob_append_string(p->pOut, "\n");
8af0255… drh 1679 }
dbda8d6… drh 1680 p->state |= AT_NEWLINE;
dbda8d6… drh 1681 break;
dbda8d6… drh 1682 }
2bb1d64… jeremy_c 1683 case TOKEN_BUL_LI: {
53b080b… drh 1684 p->mRender |= RENDER_BLOCK;
0432c38… drh 1685 if( p->wikiList!=MARKUP_UL ){
0432c38… drh 1686 if( p->wikiList ){
0432c38… drh 1687 popStackToTag(p, p->wikiList);
0432c38… drh 1688 }
0432c38… drh 1689 endAutoParagraph(p);
0432c38… drh 1690 pushStack(p, MARKUP_UL);
0432c38… drh 1691 blob_append_string(p->pOut, "<ul>");
0432c38… drh 1692 p->wikiList = MARKUP_UL;
0432c38… drh 1693 }
0432c38… drh 1694 popStackToTag(p, MARKUP_LI);
0432c38… drh 1695 startAutoParagraph(p);
0432c38… drh 1696 pushStack(p, MARKUP_LI);
0432c38… drh 1697 blob_append_string(p->pOut, "<li>");
2bb1d64… jeremy_c 1698 break;
2bb1d64… jeremy_c 1699 }
2bb1d64… jeremy_c 1700 case TOKEN_NUM_LI: {
53b080b… drh 1701 p->mRender |= RENDER_BLOCK;
0432c38… drh 1702 if( p->wikiList!=MARKUP_OL ){
0432c38… drh 1703 if( p->wikiList ){
0432c38… drh 1704 popStackToTag(p, p->wikiList);
0432c38… drh 1705 }
0432c38… drh 1706 endAutoParagraph(p);
0432c38… drh 1707 pushStack(p, MARKUP_OL);
0432c38… drh 1708 blob_append_string(p->pOut, "<ol>");
0432c38… drh 1709 p->wikiList = MARKUP_OL;
0432c38… drh 1710 }
0432c38… drh 1711 popStackToTag(p, MARKUP_LI);
0432c38… drh 1712 startAutoParagraph(p);
0432c38… drh 1713 pushStack(p, MARKUP_LI);
0432c38… drh 1714 blob_append_string(p->pOut, "<li>");
d0305b3… aku 1715 break;
d0305b3… aku 1716 }
d0305b3… aku 1717 case TOKEN_ENUM: {
53b080b… drh 1718 p->mRender |= RENDER_BLOCK;
0432c38… drh 1719 if( p->wikiList!=MARKUP_OL ){
0432c38… drh 1720 if( p->wikiList ){
0432c38… drh 1721 popStackToTag(p, p->wikiList);
0432c38… drh 1722 }
0432c38… drh 1723 endAutoParagraph(p);
0432c38… drh 1724 pushStack(p, MARKUP_OL);
0432c38… drh 1725 blob_append_string(p->pOut, "<ol>");
0432c38… drh 1726 p->wikiList = MARKUP_OL;
0432c38… drh 1727 }
0432c38… drh 1728 popStackToTag(p, MARKUP_LI);
0432c38… drh 1729 startAutoParagraph(p);
0432c38… drh 1730 pushStack(p, MARKUP_LI);
0432c38… drh 1731 blob_appendf(p->pOut, "<li value=\"%d\">", atoi(z));
d0305b3… aku 1732 break;
d0305b3… aku 1733 }
d0305b3… aku 1734 case TOKEN_INDENT: {
53b080b… drh 1735 p->mRender |= RENDER_BLOCK;
0432c38… drh 1736 assert( p->wikiList==0 );
0432c38… drh 1737 pushStack(p, MARKUP_BLOCKQUOTE);
0432c38… drh 1738 blob_append_string(p->pOut, "<blockquote>");
0432c38… drh 1739 p->wantAutoParagraph = 0;
0432c38… drh 1740 p->wikiList = MARKUP_BLOCKQUOTE;
dbda8d6… drh 1741 break;
dbda8d6… drh 1742 }
dbda8d6… drh 1743 case TOKEN_CHARACTER: {
d0305b3… aku 1744 startAutoParagraph(p);
f0d9fe2… drh 1745 if( p->state & WIKI_MARK ){
f0d9fe2… drh 1746 blob_append_string(p->pOut, "<mark>");
f0d9fe2… drh 1747 p->mRender |= RENDER_MARK;
f0d9fe2… drh 1748 }
dbda8d6… drh 1749 if( z[0]=='<' ){
d43f3e2… drh 1750 p->mRender |= RENDER_BADTAG;
48c47e1… drh 1751 blob_append_string(p->pOut, "&lt;");
dbda8d6… drh 1752 }else if( z[0]=='&' ){
d43f3e2… drh 1753 p->mRender |= RENDER_BADENTITY;
48c47e1… drh 1754 blob_append_string(p->pOut, "&amp;");
f0d9fe2… drh 1755 }
f0d9fe2… drh 1756 if( p->state & WIKI_MARK ){
f0d9fe2… drh 1757 if( fossil_isalnum(z[1]) || (z[1]=='/' && fossil_isalnum(z[2])) ){
f0d9fe2… drh 1758 int kk;
f0d9fe2… drh 1759 for(kk=2; fossil_isalnum(z[kk]); kk++){}
f0d9fe2… drh 1760 blob_append(p->pOut, &z[1], kk-1);
f0d9fe2… drh 1761 n = kk;
f0d9fe2… drh 1762 }
f0d9fe2… drh 1763 blob_append_string(p->pOut, "</mark>");
dbda8d6… drh 1764 }
dbda8d6… drh 1765 break;
dbda8d6… drh 1766 }
dbda8d6… drh 1767 case TOKEN_LINK: {
dbda8d6… drh 1768 char *zTarget;
dbda8d6… drh 1769 char *zDisplay = 0;
d0305b3… aku 1770 int i, j;
dbda8d6… drh 1771 int savedState;
f0c8693… drh 1772 char zClose[20];
90676f4… drh 1773 char cS1 = 0;
60f71ba… mistachkin 1774 int iS1 = 0;
ac3f1f2… drh 1775
d0305b3… aku 1776 startAutoParagraph(p);
53b080b… drh 1777 p->mRender |= RENDER_LINK;
dbda8d6… drh 1778 zTarget = &z[1];
dbda8d6… drh 1779 for(i=1; z[i] && z[i]!=']'; i++){
dbda8d6… drh 1780 if( z[i]=='|' && zDisplay==0 ){
dbda8d6… drh 1781 zDisplay = &z[i+1];
4615bc8… drh 1782 for(j=i; j>0 && fossil_isspace(z[j-1]); j--){}
4615bc8… drh 1783 iS1 = j;
4615bc8… drh 1784 cS1 = z[j];
4615bc8… drh 1785 z[j] = 0;
dbda8d6… drh 1786 }
dbda8d6… drh 1787 }
dbda8d6… drh 1788 z[i] = 0;
dbda8d6… drh 1789 if( zDisplay==0 ){
2d1a493… drh 1790 zDisplay = zTarget + interwiki_removable_prefix(zTarget);
2fac809… drh 1791 }else{
2fac809… drh 1792 while( fossil_isspace(*zDisplay) ) zDisplay++;
2fac809… drh 1793 }
53b080b… drh 1794 p->mRender |= wiki_resolve_hyperlink(p->pOut, p->state,
774fb77… drh 1795 zTarget, zClose, sizeof(zClose), zOrig, 0);
bf67db0… drh 1796 if( linksOnly || zClose[0]==0 || p->inVerbatim ){
4615bc8… drh 1797 if( cS1 ) z[iS1] = cS1;
4615bc8… drh 1798 if( zClose[0]!=']' ){
4615bc8… drh 1799 blob_appendf(p->pOut, "[%h]%s", zTarget, zClose);
4615bc8… drh 1800 }else{
4615bc8… drh 1801 blob_appendf(p->pOut, "%h%s", zTarget, zClose);
4615bc8… drh 1802 }
4615bc8… drh 1803 }else{
90676f4… drh 1804 savedState = p->state;
90676f4… drh 1805 p->state &= ~ALLOW_WIKI;
90676f4… drh 1806 p->state |= FONT_MARKUP_ONLY;
90676f4… drh 1807 wiki_render(p, zDisplay);
90676f4… drh 1808 p->state = savedState;
90676f4… drh 1809 blob_append(p->pOut, zClose, -1);
90676f4… drh 1810 }
dbda8d6… drh 1811 break;
dbda8d6… drh 1812 }
dbda8d6… drh 1813 case TOKEN_TEXT: {
3243e63… drh 1814 int i;
2fac809… drh 1815 for(i=0; i<n && fossil_isspace(z[i]); i++){}
3243e63… drh 1816 if( i<n ) startAutoParagraph(p);
dbda8d6… drh 1817 blob_append(p->pOut, z, n);
cf3809c… jeremy_c 1818 break;
cf3809c… jeremy_c 1819 }
cf3809c… jeremy_c 1820 case TOKEN_RAW: {
4615bc8… drh 1821 if( linksOnly ){
4615bc8… drh 1822 htmlize_to_blob(p->pOut, z, n);
4615bc8… drh 1823 }else{
4615bc8… drh 1824 blob_append(p->pOut, z, n);
4615bc8… drh 1825 }
e01aa8c… drh 1826 break;
e01aa8c… drh 1827 }
dbda8d6… drh 1828 case TOKEN_MARKUP: {
e01aa8c… drh 1829 const char *zId;
e01aa8c… drh 1830 int iDiv;
6436ecf… wyoung 1831 (void)parseMarkup(&markup, z);
d8586b5… drh 1832
d8586b5… drh 1833 /* Convert <title> to <h1 align='center'> */
d8586b5… drh 1834 if( markup.iCode==MARKUP_TITLE && !p->inVerbatim ){
d8586b5… drh 1835 markup.iCode = MARKUP_H1;
d8586b5… drh 1836 markup.nAttr = 1;
6436ecf… wyoung 1837 markup.aAttr[0].iACode = ATTR_ALIGN;
d8586b5… drh 1838 markup.aAttr[0].zValue = "center";
d8586b5… drh 1839 markup.aAttr[0].cTerm = 0;
d8586b5… drh 1840 }
e01aa8c… drh 1841
e01aa8c… drh 1842 /* Markup of the form </div id=ID> where there is a matching
4f8c897… drh 1843 ** ID somewhere on the stack. Exit any contained verbatim.
4f8c897… drh 1844 ** Pop the stack up to the matching <div>. Discard the </div>
e01aa8c… drh 1845 */
e01aa8c… drh 1846 if( markup.iCode==MARKUP_DIV && markup.endTag &&
e01aa8c… drh 1847 (zId = markupId(&markup))!=0 &&
e01aa8c… drh 1848 (iDiv = findTagWithId(p, MARKUP_DIV, zId))>=0
e01aa8c… drh 1849 ){
e01aa8c… drh 1850 if( p->inVerbatim ){
e01aa8c… drh 1851 p->inVerbatim = 0;
e01aa8c… drh 1852 p->state = p->preVerbState;
48c47e1… drh 1853 blob_append_string(p->pOut, "</pre>");
e01aa8c… drh 1854 }
e01aa8c… drh 1855 while( p->nStack>iDiv+1 ) popStack(p);
e01aa8c… drh 1856 if( p->aStack[iDiv].allowWiki ){
e01aa8c… drh 1857 p->state |= ALLOW_WIKI;
e01aa8c… drh 1858 }else{
e01aa8c… drh 1859 p->state &= ~ALLOW_WIKI;
e01aa8c… drh 1860 }
e01aa8c… drh 1861 assert( p->nStack==iDiv+1 );
e01aa8c… drh 1862 p->nStack--;
e01aa8c… drh 1863 }else
e01aa8c… drh 1864
e01aa8c… drh 1865 /* If within <verbatim id=ID> ignore everything other than
e01aa8c… drh 1866 ** </verbatim id=ID> and the </dev id=ID2> above.
b9d3b42… drh 1867 */
dbda8d6… drh 1868 if( p->inVerbatim ){
dbda8d6… drh 1869 if( endVerbatim(p, &markup) ){
dbda8d6… drh 1870 p->inVerbatim = 0;
dbda8d6… drh 1871 p->state = p->preVerbState;
48c47e1… drh 1872 blob_append_string(p->pOut, "</pre>");
e01aa8c… drh 1873 }else{
e01aa8c… drh 1874 unparseMarkup(&markup);
48c47e1… drh 1875 blob_append_string(p->pOut, "&lt;");
e01aa8c… drh 1876 n = 1;
e01aa8c… drh 1877 }
e01aa8c… drh 1878 }else
e01aa8c… drh 1879
e01aa8c… drh 1880 /* Render invalid markup literally. The markup appears in the
e01aa8c… drh 1881 ** final output as plain text.
e01aa8c… drh 1882 */
e01aa8c… drh 1883 if( markup.iCode==MARKUP_INVALID ){
53b080b… drh 1884 p->mRender |= RENDER_BADTAG;
e01aa8c… drh 1885 unparseMarkup(&markup);
e01aa8c… drh 1886 startAutoParagraph(p);
f0d9fe2… drh 1887 if( p->state & WIKI_MARK ){
f0d9fe2… drh 1888 p->mRender |= RENDER_MARK;
f0d9fe2… drh 1889 blob_append_string(p->pOut, "<mark>");
f0d9fe2… drh 1890 htmlize_to_blob(p->pOut, z, n);
f0d9fe2… drh 1891 blob_append_string(p->pOut, "</mark>");
f0d9fe2… drh 1892 }else{
f0d9fe2… drh 1893 blob_append_string(p->pOut, "&lt;");
f0d9fe2… drh 1894 htmlize_to_blob(p->pOut, z+1, n-1);
f0d9fe2… drh 1895 }
e01aa8c… drh 1896 }else
e01aa8c… drh 1897
e01aa8c… drh 1898 /* If the markup is not font-change markup ignore it if the
e01aa8c… drh 1899 ** font-change-only flag is set.
e01aa8c… drh 1900 */
e01aa8c… drh 1901 if( (markup.iType&MUTYPE_FONT)==0 && (p->state & FONT_MARKUP_ONLY)!=0 ){
e01aa8c… drh 1902 /* Do nothing */
e01aa8c… drh 1903 }else
e01aa8c… drh 1904
e01aa8c… drh 1905 if( markup.iCode==MARKUP_NOWIKI ){
dbda8d6… drh 1906 if( markup.endTag ){
dbda8d6… drh 1907 p->state |= ALLOW_WIKI;
dbda8d6… drh 1908 }else{
d0305b3… aku 1909 p->state &= ~ALLOW_WIKI;
dbda8d6… drh 1910 }
e01aa8c… drh 1911 }else
e01aa8c… drh 1912
e01aa8c… drh 1913 /* Generate end-tags */
e01aa8c… drh 1914 if( markup.endTag ){
dbda8d6… drh 1915 popStackToTag(p, markup.iCode);
e01aa8c… drh 1916 }else
e01aa8c… drh 1917
e01aa8c… drh 1918 /* Push <div> markup onto the stack together with the id=ID attribute.
e01aa8c… drh 1919 */
6436ecf… wyoung 1920 if( markup.iCode==MARKUP_DIV && attributeValue(&markup, ATTR_ID)!=0 ){
e01aa8c… drh 1921 pushStackWithId(p, markup.iCode, markupId(&markup),
e01aa8c… drh 1922 (p->state & ALLOW_WIKI)!=0);
e01aa8c… drh 1923 }else
e01aa8c… drh 1924
e01aa8c… drh 1925 /* Enter <verbatim> processing. With verbatim enabled, all other
e01aa8c… drh 1926 ** markup other than the corresponding end-tag with the same ID is
b9d3b42… drh 1927 ** ignored.
e01aa8c… drh 1928 */
e01aa8c… drh 1929 if( markup.iCode==MARKUP_VERBATIM ){
e2bdc10… danield 1930 int ii; /*, vAttrDidAppend=0;*/
1fc2df9… drh 1931 const char *zClass = 0;
b9d3b42… drh 1932 p->zVerbatimId = 0;
dbda8d6… drh 1933 p->inVerbatim = 1;
dbda8d6… drh 1934 p->preVerbState = p->state;
dbda8d6… drh 1935 p->state &= ~ALLOW_WIKI;
bf67db0… drh 1936 for(ii=0; ii<markup.nAttr; ii++){
bf67db0… drh 1937 if( markup.aAttr[ii].iACode == ATTR_ID ){
bf67db0… drh 1938 p->zVerbatimId = markup.aAttr[ii].zValue;
4f8c897… drh 1939 }else if( markup.aAttr[ii].iACode==ATTR_TYPE ){
1fc2df9… drh 1940 zClass = markup.aAttr[ii].zValue;
4f8c897… drh 1941 }else if( markup.aAttr[ii].iACode==ATTR_LINKS
4f8c897… drh 1942 && !is_false(markup.aAttr[ii].zValue) ){
4f8c897… drh 1943 p->state |= ALLOW_LINKS;
b9d3b42… drh 1944 }
b9d3b42… drh 1945 }
1fc2df9… drh 1946 endAutoParagraph(p);
1fc2df9… drh 1947 if( zClass==0 ){
48c47e1… drh 1948 blob_append_string(p->pOut, "<pre class='verbatim'>");
1fc2df9… drh 1949 }else if( strncmp(zClass,"pikchr",6)==0 &&
1fc2df9… drh 1950 (fossil_isspace(zClass[6]) || zClass[6]==0) ){
1fc2df9… drh 1951 n += wiki_process_pikchr(p, z+n, zClass);
1fc2df9… drh 1952 p->inVerbatim = 0;
1fc2df9… drh 1953 p->state = p->preVerbState;
1fc2df9… drh 1954 }else{
1fc2df9… drh 1955 blob_appendf(p->pOut, "<pre name='code' class='%h'>",
1fc2df9… drh 1956 zClass);
3243e63… drh 1957 }
d0305b3… aku 1958 p->wantAutoParagraph = 0;
e01aa8c… drh 1959 }else
e01aa8c… drh 1960 if( markup.iType==MUTYPE_LI ){
dbda8d6… drh 1961 if( backupToType(p, MUTYPE_LIST)==0 ){
3243e63… drh 1962 endAutoParagraph(p);
dbda8d6… drh 1963 pushStack(p, MARKUP_UL);
48c47e1… drh 1964 blob_append_string(p->pOut, "<ul>");
dbda8d6… drh 1965 }
dbda8d6… drh 1966 pushStack(p, MARKUP_LI);
dbda8d6… drh 1967 renderMarkup(p->pOut, &markup);
e01aa8c… drh 1968 }else
e01aa8c… drh 1969 if( markup.iType==MUTYPE_TR ){
dbda8d6… drh 1970 if( backupToType(p, MUTYPE_TABLE) ){
dbda8d6… drh 1971 pushStack(p, MARKUP_TR);
dbda8d6… drh 1972 renderMarkup(p->pOut, &markup);
dbda8d6… drh 1973 }
e01aa8c… drh 1974 }else
e01aa8c… drh 1975 if( markup.iType==MUTYPE_TD ){
dbda8d6… drh 1976 if( backupToType(p, MUTYPE_TABLE|MUTYPE_TR) ){
dbda8d6… drh 1977 if( stackTopType(p)==MUTYPE_TABLE ){
dbda8d6… drh 1978 pushStack(p, MARKUP_TR);
48c47e1… drh 1979 blob_append_string(p->pOut, "<tr>");
e01aa8c… drh 1980 }
8a1ba49… wyoung 1981 p->wantAutoParagraph = 0;
e01aa8c… drh 1982 pushStack(p, markup.iCode);
e01aa8c… drh 1983 renderMarkup(p->pOut, &markup);
e01aa8c… drh 1984 }
e01aa8c… drh 1985 }else
e01aa8c… drh 1986 if( markup.iType==MUTYPE_HYPERLINK ){
18c310a… drh 1987 if( !isButtonHyperlink(p, &markup, z, &n) ){
18c310a… drh 1988 popStackToTag(p, markup.iCode);
18c310a… drh 1989 startAutoParagraph(p);
18c310a… drh 1990 renderMarkup(p->pOut, &markup);
18c310a… drh 1991 pushStack(p, markup.iCode);
18c310a… drh 1992 }
e01aa8c… drh 1993 }else
e01aa8c… drh 1994 {
d0305b3… aku 1995 if( markup.iType==MUTYPE_FONT ){
d0305b3… aku 1996 startAutoParagraph(p);
3243e63… drh 1997 }else if( markup.iType==MUTYPE_BLOCK || markup.iType==MUTYPE_LIST ){
53b080b… drh 1998 p->mRender |= RENDER_BLOCKTAG;
d0305b3… aku 1999 p->wantAutoParagraph = 0;
3243e63… drh 2000 }
3243e63… drh 2001 if( markup.iCode==MARKUP_HR
3243e63… drh 2002 || markup.iCode==MARKUP_H1
3243e63… drh 2003 || markup.iCode==MARKUP_H2
3243e63… drh 2004 || markup.iCode==MARKUP_H3
3243e63… drh 2005 || markup.iCode==MARKUP_H4
3243e63… drh 2006 || markup.iCode==MARKUP_H5
3243e63… drh 2007 || markup.iCode==MARKUP_P
3243e63… drh 2008 ){
3243e63… drh 2009 endAutoParagraph(p);
d0305b3… aku 2010 }
dbda8d6… drh 2011 if( (markup.iType & MUTYPE_STACK )!=0 ){
dbda8d6… drh 2012 pushStack(p, markup.iCode);
dbda8d6… drh 2013 }
dbda8d6… drh 2014 renderMarkup(p->pOut, &markup);
dbda8d6… drh 2015 }
dbda8d6… drh 2016 break;
dbda8d6… drh 2017 }
dbda8d6… drh 2018 }
dbda8d6… drh 2019 z += n;
dbda8d6… drh 2020 }
dbda8d6… drh 2021 }
dbda8d6… drh 2022
dbda8d6… drh 2023 /*
dbda8d6… drh 2024 ** Transform the text in the pIn blob. Write the results
dbda8d6… drh 2025 ** into the pOut blob. The pOut blob should already be
dbda8d6… drh 2026 ** initialized. The output is merely appended to pOut.
d0305b3… aku 2027 ** If pOut is NULL, then the output is appended to the CGI
d0305b3… aku 2028 ** reply.
53b080b… drh 2029 **
53b080b… drh 2030 ** Return a mask of RENDER_ flags indicating what happened.
d0305b3… aku 2031 */
53b080b… drh 2032 int wiki_convert(Blob *pIn, Blob *pOut, int flags){
dbda8d6… drh 2033 Renderer renderer;
b9d3b42… drh 2034
dbda8d6… drh 2035 memset(&renderer, 0, sizeof(renderer));
90676f4… drh 2036 renderer.renderFlags = flags;
18c310a… drh 2037 renderer.state = ALLOW_WIKI|AT_NEWLINE|AT_PARAGRAPH|flags;
d0305b3… aku 2038 if( flags & WIKI_INLINE ){
d0305b3… aku 2039 renderer.wantAutoParagraph = 0;
d0305b3… aku 2040 }else{
d0305b3… aku 2041 renderer.wantAutoParagraph = 1;
cf3809c… jeremy_c 2042 }
7c2577b… drh 2043 if( wikiUsesHtml() ){
4615bc8… drh 2044 renderer.state |= WIKI_HTMLONLY;
d0305b3… aku 2045 }
d0305b3… aku 2046 if( pOut ){
d0305b3… aku 2047 renderer.pOut = pOut;
d0305b3… aku 2048 }else{
d0305b3… aku 2049 renderer.pOut = cgi_output_blob();
d0305b3… aku 2050 }
dbda8d6… drh 2051
9b800ee… mistachkin 2052 blob_to_utf8_no_bom(pIn, 0);
70b4f10… jan.nijtmans 2053 wiki_render(&renderer, blob_str(pIn));
d0305b3… aku 2054 endAutoParagraph(&renderer);
dbda8d6… drh 2055 while( renderer.nStack ){
dbda8d6… drh 2056 popStack(&renderer);
dbda8d6… drh 2057 }
48c47e1… drh 2058 blob_append_char(renderer.pOut, '\n');
dbda8d6… drh 2059 free(renderer.aStack);
53b080b… drh 2060 return renderer.mRender;
dbda8d6… drh 2061 }
dbda8d6… drh 2062
dbda8d6… drh 2063 /*
dbda8d6… drh 2064 ** COMMAND: test-wiki-render
18c310a… drh 2065 **
8bfd995… rberteig 2066 ** Usage: %fossil test-wiki-render FILE [OPTIONS]
8bfd995… rberteig 2067 **
fcb20df… drh 2068 ** Translate the input FILE from Fossil-wiki into HTML and write
fcb20df… drh 2069 ** the resulting HTML on standard output.
fcb20df… drh 2070 **
18c310a… drh 2071 ** Options:
18c310a… drh 2072 ** --buttons Set the WIKI_BUTTONS flag
3ea3cd5… drh 2073 ** --dark-pikchr Render pikchrs in dark mode
f0d9fe2… drh 2074 ** --flow Render as text using comment_format
4615bc8… drh 2075 ** --htmlonly Set the WIKI_HTMLONLY flag
4615bc8… drh 2076 ** --inline Set the WIKI_INLINE flag
3ea3cd5… drh 2077 ** --linksonly Set the WIKI_LINKSONLY flag
d43f3e2… drh 2078 ** -m TEXT Use TEXT in place of the content of FILE
f0d9fe2… drh 2079 ** --mark Add <mark>...</mark> around problems
3ea3cd5… drh 2080 ** --nobadlinks Set the WIKI_NOBADLINKS flag
f0d9fe2… drh 2081 ** --text Run the output through html_to_plaintext()
f0d9fe2… drh 2082 ** --type Break down the return code from wiki_convert()
dbda8d6… drh 2083 */
dbda8d6… drh 2084 void test_wiki_render(void){
dbda8d6… drh 2085 Blob in, out;
18c310a… drh 2086 int flags = 0;
3ea3cd5… drh 2087 int bText;
f0d9fe2… drh 2088 int bFlow = 0;
53b080b… drh 2089 int showType = 0;
53b080b… drh 2090 int mType;
d43f3e2… drh 2091 const char *zIn;
18c310a… drh 2092 if( find_option("buttons",0,0)!=0 ) flags |= WIKI_BUTTONS;
4615bc8… drh 2093 if( find_option("htmlonly",0,0)!=0 ) flags |= WIKI_HTMLONLY;
4615bc8… drh 2094 if( find_option("linksonly",0,0)!=0 ) flags |= WIKI_LINKSONLY;
4615bc8… drh 2095 if( find_option("nobadlinks",0,0)!=0 ) flags |= WIKI_NOBADLINKS;
4615bc8… drh 2096 if( find_option("inline",0,0)!=0 ) flags |= WIKI_INLINE;
f0d9fe2… drh 2097 if( find_option("mark",0,0)!=0 ) flags |= WIKI_MARK;
aac2a35… stephan 2098 if( find_option("dark-pikchr",0,0)!=0 ){
aac2a35… stephan 2099 pikchr_to_html_add_flags( PIKCHR_PROCESS_DARK_MODE );
aac2a35… stephan 2100 }
3ea3cd5… drh 2101 bText = find_option("text",0,0)!=0;
f0d9fe2… drh 2102 bFlow = find_option("flow",0,0)!=0;
53b080b… drh 2103 showType = find_option("type",0,0)!=0;
d43f3e2… drh 2104 zIn = find_option("msg","m",1);
0ac64da… drh 2105 db_find_and_open_repository(OPEN_OK_NOT_FOUND|OPEN_SUBSTITUTE,0);
18c310a… drh 2106 verify_all_options();
d43f3e2… drh 2107 if( (zIn==0 && g.argc!=3) || (zIn!=0 && g.argc!=2) ) usage("FILE");
dbda8d6… drh 2108 blob_zero(&out);
d43f3e2… drh 2109 if( zIn ){
d43f3e2… drh 2110 blob_init(&in, zIn, -1);
d43f3e2… drh 2111 }else{
d43f3e2… drh 2112 blob_read_from_file(&in, g.argv[2], ExtFILE);
d43f3e2… drh 2113 }
53b080b… drh 2114 mType = wiki_convert(&in, &out, flags);
3ea3cd5… drh 2115 if( bText ){
3ea3cd5… drh 2116 Blob txt;
d62ca2b… drh 2117 int htot = HTOT_TRIM;
f0d9fe2… drh 2118 if( terminal_is_vt100() ) htot |= HTOT_VT100;
d62ca2b… drh 2119 if( bFlow ) htot |= HTOT_FLOW;
3ea3cd5… drh 2120 blob_init(&txt, 0, 0);
f0d9fe2… drh 2121 html_to_plaintext(blob_str(&out),&txt, htot);
3ea3cd5… drh 2122 blob_reset(&out);
3ea3cd5… drh 2123 out = txt;
3ea3cd5… drh 2124 }
f0d9fe2… drh 2125 if( bFlow ){
f0d9fe2… drh 2126 fossil_print(" ");
f0d9fe2… drh 2127 comment_print(blob_str(&out), 0, 3, terminal_get_width(80)-3,
f0d9fe2… drh 2128 get_comment_format());
f0d9fe2… drh 2129 }else{
f0d9fe2… drh 2130 blob_write_to_file(&out, "-");
f0d9fe2… drh 2131 }
53b080b… drh 2132 if( showType ){
53b080b… drh 2133 fossil_print("%.*c\nResult Codes:", terminal_get_width(80)-1, '*');
53b080b… drh 2134 if( mType & RENDER_LINK ) fossil_print(" LINK");
53b080b… drh 2135 if( mType & RENDER_ENTITY ) fossil_print(" ENTITY");
53b080b… drh 2136 if( mType & RENDER_TAG ) fossil_print(" TAG");
53b080b… drh 2137 if( mType & RENDER_BLOCKTAG ) fossil_print(" BLOCKTAG");
53b080b… drh 2138 if( mType & RENDER_BLOCK ) fossil_print(" BLOCK");
f0d9fe2… drh 2139 if( mType & RENDER_MARK ) fossil_print(" MARK");
53b080b… drh 2140 if( mType & RENDER_BADLINK ) fossil_print(" BADLINK");
53b080b… drh 2141 if( mType & RENDER_BADTARGET ) fossil_print(" BADTARGET");
53b080b… drh 2142 if( mType & RENDER_BADTAG ) fossil_print(" BADTAG");
53b080b… drh 2143 if( mType & RENDER_BADENTITY ) fossil_print(" BADENTITY");
53b080b… drh 2144 if( mType & RENDER_BADHTML ) fossil_print(" BADHTML");
53b080b… drh 2145 if( mType & RENDER_ERROR ) fossil_print(" ERROR");
53b080b… drh 2146 fossil_print("\n");
53b080b… drh 2147 }
fcb20df… drh 2148 }
fcb20df… drh 2149
fcb20df… drh 2150 /*
fcb20df… drh 2151 ** COMMAND: test-markdown-render
fcb20df… drh 2152 **
7192ce1… stephan 2153 ** Usage: %fossil test-markdown-render TEXT ...
f4e6efb… drh 2154 **
7192ce1… stephan 2155 ** Render markdown in TEXT as HTML on stdout, where TEXT
7192ce1… stephan 2156 ** may be a file name or a markdown-formatted string.
7192ce1… stephan 2157 **
f4e6efb… drh 2158 ** Options:
fcb20df… drh 2159 **
3ea3cd5… drh 2160 ** --dark-pikchr Render pikchrs in dark mode
3990518… george 2161 ** --lint-footnotes Print stats for footnotes-related issues
3ea3cd5… drh 2162 ** --safe Restrict the output to use only "safe" HTML
3ea3cd5… drh 2163 ** --text Run the output through html_to_plaintext().
fcb20df… drh 2164 */
fcb20df… drh 2165 void test_markdown_render(void){
fcb20df… drh 2166 Blob in, out;
f4e6efb… drh 2167 int i;
3ea3cd5… drh 2168 int bSafe = 0, bFnLint = 0, bText = 0;
fcb20df… drh 2169 db_find_and_open_repository(OPEN_OK_NOT_FOUND|OPEN_SUBSTITUTE,0);
382f373… drh 2170 bSafe = find_option("safe",0,0)!=0;
3990518… george 2171 bFnLint = find_option("lint-footnotes",0,0)!=0;
aac2a35… stephan 2172 if( find_option("dark-pikchr",0,0)!=0 ){
aac2a35… stephan 2173 pikchr_to_html_add_flags( PIKCHR_PROCESS_DARK_MODE );
aac2a35… stephan 2174 }
3ea3cd5… drh 2175 bText = find_option("text",0,0)!=0;
fcb20df… drh 2176 verify_all_options();
f4e6efb… drh 2177 for(i=2; i<g.argc; i++){
f4e6efb… drh 2178 blob_zero(&out);
7192ce1… stephan 2179 if(file_isfile(g.argv[i], ExtFILE)){
7192ce1… stephan 2180 blob_read_from_file(&in, g.argv[i], ExtFILE);
7192ce1… stephan 2181 if( g.argc>3 ){
7192ce1… stephan 2182 fossil_print("<!------ %h ------->\n", g.argv[i]);
7192ce1… stephan 2183 }
7192ce1… stephan 2184 }else{
7192ce1… stephan 2185 blob_init(&in, g.argv[i], -1);
7192ce1… stephan 2186 if( g.argc>3 ){
7192ce1… stephan 2187 fossil_print("<!------ script #%d ------->\n", i-1);
7192ce1… stephan 2188 }
f4e6efb… drh 2189 }
f4e6efb… drh 2190 markdown_to_html(&in, 0, &out);
89b6dda… drh 2191 safe_html_context( bSafe ? DOCSRC_UNTRUSTED : DOCSRC_TRUSTED );
89b6dda… drh 2192 safe_html(&out);
3ea3cd5… drh 2193 if( bText ){
3ea3cd5… drh 2194 Blob txt;
3ea3cd5… drh 2195 blob_init(&txt, 0, 0);
fd59eb1… drh 2196 html_to_plaintext(blob_str(&out), &txt, HTOT_VT100);
3ea3cd5… drh 2197 blob_reset(&out);
3ea3cd5… drh 2198 out = txt;
3ea3cd5… drh 2199 }
f4e6efb… drh 2200 blob_write_to_file(&out, "-");
f4e6efb… drh 2201 blob_reset(&in);
f4e6efb… drh 2202 blob_reset(&out);
3990518… george 2203 }
3990518… george 2204 if( bFnLint && (g.ftntsIssues[0] || g.ftntsIssues[1]
3990518… george 2205 || g.ftntsIssues[2] || g.ftntsIssues[3] )){
3990518… george 2206 fossil_fatal("There were issues with footnotes:\n"
3990518… george 2207 " %8d misreference%s\n"
3990518… george 2208 " %8d unreferenced\n"
f3adbd8… drh 2209 " %8d split\n"
3990518… george 2210 " %8d overnested",
3990518… george 2211 g.ftntsIssues[0], g.ftntsIssues[0]==1?"":"s",
3990518… george 2212 g.ftntsIssues[1], g.ftntsIssues[2], g.ftntsIssues[3]);
f4e6efb… drh 2213 }
f88e2e7… drh 2214 }
f88e2e7… drh 2215
f88e2e7… drh 2216 /*
f88e2e7… drh 2217 ** Search for a <title>...</title> at the beginning of a wiki page.
f88e2e7… drh 2218 ** Return true (nonzero) if a title is found. Return zero if there is
f88e2e7… drh 2219 ** not title.
b9d3b42… drh 2220 **
f88e2e7… drh 2221 ** If a title is found, initialize the pTitle blob to be the content
f88e2e7… drh 2222 ** of the title and initialize pTail to be the text that follows the
f88e2e7… drh 2223 ** title.
f88e2e7… drh 2224 */
f88e2e7… drh 2225 int wiki_find_title(Blob *pIn, Blob *pTitle, Blob *pTail){
f88e2e7… drh 2226 char *z;
f88e2e7… drh 2227 int i;
f88e2e7… drh 2228 int iStart;
9b800ee… mistachkin 2229 blob_to_utf8_no_bom(pIn, 0);
70b4f10… jan.nijtmans 2230 z = blob_str(pIn);
2fac809… drh 2231 for(i=0; fossil_isspace(z[i]); i++){}
f88e2e7… drh 2232 if( z[i]!='<' ) return 0;
f88e2e7… drh 2233 i++;
f88e2e7… drh 2234 if( strncmp(&z[i],"title>", 6)!=0 ) return 0;
afc25dd… mistachkin 2235 for(iStart=i+6; fossil_isspace(z[iStart]); iStart++){}
f88e2e7… drh 2236 for(i=iStart; z[i] && (z[i]!='<' || strncmp(&z[i],"</title>",8)!=0); i++){}
afc25dd… mistachkin 2237 if( strncmp(&z[i],"</title>",8)!=0 ){
afc25dd… mistachkin 2238 blob_init(pTitle, 0, 0);
afc25dd… mistachkin 2239 blob_init(pTail, &z[iStart], -1);
afc25dd… mistachkin 2240 return 1;
afc25dd… mistachkin 2241 }
afc25dd… mistachkin 2242 if( i-iStart>0 ){
afc25dd… mistachkin 2243 blob_init(pTitle, &z[iStart], i-iStart);
afc25dd… mistachkin 2244 }else{
afc25dd… mistachkin 2245 blob_init(pTitle, 0, 0);
afc25dd… mistachkin 2246 }
f88e2e7… drh 2247 blob_init(pTail, &z[i+8], -1);
f88e2e7… drh 2248 return 1;
7c2577b… drh 2249 }
7c2577b… drh 2250
7c2577b… drh 2251 /*
7c2577b… drh 2252 ** Parse text looking for wiki hyperlinks in one of the formats:
7c2577b… drh 2253 **
7c2577b… drh 2254 ** [target]
7c2577b… drh 2255 ** [target|...]
7c2577b… drh 2256 **
7c2577b… drh 2257 ** Where "target" can be either an artifact ID prefix or a wiki page
7c2577b… drh 2258 ** name. For each such hyperlink found, add an entry to the
7c2577b… drh 2259 ** backlink table.
53b080b… drh 2260 **
53b080b… drh 2261 ** The return value is a mask of RENDER_ flags.
7c2577b… drh 2262 */
53b080b… drh 2263 int wiki_extract_links(
7c2577b… drh 2264 char *z, /* The wiki text from which to extract links */
ae1dac8… drh 2265 Backlink *pBklnk, /* Backlink extraction context */
7c2577b… drh 2266 int flags /* wiki parsing flags */
7c2577b… drh 2267 ){
7c2577b… drh 2268 Renderer renderer;
7c2577b… drh 2269 int tokenType;
7c2577b… drh 2270 ParsedMarkup markup;
7c2577b… drh 2271 int n;
4615bc8… drh 2272 int wikiHtmlOnly = 0;
7c2577b… drh 2273
7c2577b… drh 2274 memset(&renderer, 0, sizeof(renderer));
7c2577b… drh 2275 renderer.state = ALLOW_WIKI|AT_NEWLINE|AT_PARAGRAPH;
7c2577b… drh 2276 if( wikiUsesHtml() ){
4615bc8… drh 2277 renderer.state |= WIKI_HTMLONLY;
4615bc8… drh 2278 wikiHtmlOnly = 1;
7c2577b… drh 2279 }
7c2577b… drh 2280
7c2577b… drh 2281 while( z[0] ){
4615bc8… drh 2282 if( wikiHtmlOnly ){
7c2577b… drh 2283 n = nextRawToken(z, &renderer, &tokenType);
7c2577b… drh 2284 }else{
7c2577b… drh 2285 n = nextWikiToken(z, &renderer, &tokenType);
7c2577b… drh 2286 }
7c2577b… drh 2287 switch( tokenType ){
7c2577b… drh 2288 case TOKEN_LINK: {
7c2577b… drh 2289 char *zTarget;
ae1dac8… drh 2290 int i;
7c2577b… drh 2291
7c2577b… drh 2292 zTarget = &z[1];
7c2577b… drh 2293 for(i=0; zTarget[i] && zTarget[i]!='|' && zTarget[i]!=']'; i++){}
7c2577b… drh 2294 while(i>1 && zTarget[i-1]==' '){ i--; }
ae1dac8… drh 2295 backlink_create(pBklnk, zTarget, i);
7c2577b… drh 2296 break;
7c2577b… drh 2297 }
7c2577b… drh 2298 case TOKEN_MARKUP: {
7c2577b… drh 2299 const char *zId;
7c2577b… drh 2300 int iDiv;
7c2577b… drh 2301 parseMarkup(&markup, z);
7c2577b… drh 2302
7c2577b… drh 2303 /* Markup of the form </div id=ID> where there is a matching
7c2577b… drh 2304 ** ID somewhere on the stack. Exit the verbatim if were are in
7c2577b… drh 2305 ** it. Pop the stack up to the matching <div>. Discard the
7c2577b… drh 2306 ** </div>
7c2577b… drh 2307 */
7c2577b… drh 2308 if( markup.iCode==MARKUP_DIV && markup.endTag &&
7c2577b… drh 2309 (zId = markupId(&markup))!=0 &&
7c2577b… drh 2310 (iDiv = findTagWithId(&renderer, MARKUP_DIV, zId))>=0
7c2577b… drh 2311 ){
7c2577b… drh 2312 if( renderer.inVerbatim ){
7c2577b… drh 2313 renderer.inVerbatim = 0;
7c2577b… drh 2314 renderer.state = renderer.preVerbState;
7c2577b… drh 2315 }
7c2577b… drh 2316 while( renderer.nStack>iDiv+1 ) popStack(&renderer);
7c2577b… drh 2317 if( renderer.aStack[iDiv].allowWiki ){
7c2577b… drh 2318 renderer.state |= ALLOW_WIKI;
7c2577b… drh 2319 }else{
7c2577b… drh 2320 renderer.state &= ~ALLOW_WIKI;
7c2577b… drh 2321 }
7c2577b… drh 2322 renderer.nStack--;
7c2577b… drh 2323 }else
7c2577b… drh 2324
7c2577b… drh 2325 /* If within <verbatim id=ID> ignore everything other than
7c2577b… drh 2326 ** </verbatim id=ID> and the </dev id=ID2> above.
7c2577b… drh 2327 */
7c2577b… drh 2328 if( renderer.inVerbatim ){
7c2577b… drh 2329 if( endVerbatim(&renderer, &markup) ){
7c2577b… drh 2330 renderer.inVerbatim = 0;
7c2577b… drh 2331 renderer.state = renderer.preVerbState;
7c2577b… drh 2332 }else{
7c2577b… drh 2333 n = 1;
7c2577b… drh 2334 }
7c2577b… drh 2335 }else
7c2577b… drh 2336
7c2577b… drh 2337 /* Render invalid markup literally. The markup appears in the
7c2577b… drh 2338 ** final output as plain text.
7c2577b… drh 2339 */
7c2577b… drh 2340 if( markup.iCode==MARKUP_INVALID ){
7c2577b… drh 2341 n = 1;
7c2577b… drh 2342 }else
7c2577b… drh 2343
7c2577b… drh 2344 /* If the markup is not font-change markup ignore it if the
7c2577b… drh 2345 ** font-change-only flag is set.
7c2577b… drh 2346 */
7c2577b… drh 2347 if( (markup.iType&MUTYPE_FONT)==0 &&
7c2577b… drh 2348 (renderer.state & FONT_MARKUP_ONLY)!=0 ){
7c2577b… drh 2349 /* Do nothing */
7c2577b… drh 2350 }else
7c2577b… drh 2351
7c2577b… drh 2352 if( markup.iCode==MARKUP_NOWIKI ){
7c2577b… drh 2353 if( markup.endTag ){
7c2577b… drh 2354 renderer.state |= ALLOW_WIKI;
7c2577b… drh 2355 }else{
7c2577b… drh 2356 renderer.state &= ~ALLOW_WIKI;
7c2577b… drh 2357 }
7c2577b… drh 2358 }else
7c2577b… drh 2359
7c2577b… drh 2360 /* Generate end-tags */
7c2577b… drh 2361 if( markup.endTag ){
7c2577b… drh 2362 popStackToTag(&renderer, markup.iCode);
7c2577b… drh 2363 }else
7c2577b… drh 2364
7c2577b… drh 2365 /* Push <div> markup onto the stack together with the id=ID attribute.
7c2577b… drh 2366 */
016334c… stephan 2367 if( markup.iCode==MARKUP_DIV ){
7c2577b… drh 2368 pushStackWithId(&renderer, markup.iCode, markupId(&markup),
7c2577b… drh 2369 (renderer.state & ALLOW_WIKI)!=0);
7c2577b… drh 2370 }else
7c2577b… drh 2371
7c2577b… drh 2372 /* Enter <verbatim> processing. With verbatim enabled, all other
7c2577b… drh 2373 ** markup other than the corresponding end-tag with the same ID is
7c2577b… drh 2374 ** ignored.
7c2577b… drh 2375 */
7c2577b… drh 2376 if( markup.iCode==MARKUP_VERBATIM ){
d8bbe4a… drh 2377 int vAttrIdx;
7c2577b… drh 2378 renderer.zVerbatimId = 0;
7c2577b… drh 2379 renderer.inVerbatim = 1;
7c2577b… drh 2380 renderer.preVerbState = renderer.state;
7c2577b… drh 2381 renderer.state &= ~ALLOW_WIKI;
7c2577b… drh 2382 for (vAttrIdx = 0; vAttrIdx < markup.nAttr; vAttrIdx++){
7c2577b… drh 2383 if( markup.aAttr[vAttrIdx].iACode == ATTR_ID ){
7c2577b… drh 2384 renderer.zVerbatimId = markup.aAttr[0].zValue;
7c2577b… drh 2385 }
7c2577b… drh 2386 }
7c2577b… drh 2387 renderer.wantAutoParagraph = 0;
7c2577b… drh 2388 }
7c2577b… drh 2389
7c2577b… drh 2390 /* Restore the input text to its original configuration
7c2577b… drh 2391 */
7c2577b… drh 2392 unparseMarkup(&markup);
7c2577b… drh 2393 break;
7c2577b… drh 2394 }
7c2577b… drh 2395 default: {
7c2577b… drh 2396 break;
7c2577b… drh 2397 }
7c2577b… drh 2398 }
7c2577b… drh 2399 z += n;
d8ec765… drh 2400 }
d8ec765… drh 2401 free(renderer.aStack);
53b080b… drh 2402 return renderer.mRender;
65870e8… drh 2403 }
65870e8… drh 2404
65870e8… drh 2405 /*
9cf90a4… drh 2406 ** Return the length, in bytes, of the HTML token that z is pointing to.
65870e8… drh 2407 */
9cf90a4… drh 2408 int html_token_length(const char *z){
65870e8… drh 2409 int n;
0f96ffb… drh 2410 char c;
0f96ffb… drh 2411 if( (c=z[0])=='<' ){
3f99bca… drh 2412 n = html_tag_length(z);
65870e8… drh 2413 if( n<=0 ) n = 1;
0f96ffb… drh 2414 }else if( fossil_isspace(c) ){
65870e8… drh 2415 for(n=1; z[n] && fossil_isspace(z[n]); n++){}
0f96ffb… drh 2416 }else if( c=='&' ){
0f96ffb… drh 2417 n = z[1]=='#' ? 2 : 1;
0f96ffb… drh 2418 while( fossil_isalnum(z[n]) ) n++;
0f96ffb… drh 2419 if( z[n]==';' ) n++;
65870e8… drh 2420 }else{
0f96ffb… drh 2421 n = 1;
0f96ffb… drh 2422 for(n=1; 1; n++){
0f96ffb… drh 2423 if( (c = z[n]) > '<' ) continue;
0f96ffb… drh 2424 if( c=='<' || c=='&' || fossil_isspace(c) || c==0 ) break;
0f96ffb… drh 2425 }
65870e8… drh 2426 }
65870e8… drh 2427 return n;
9cf90a4… drh 2428 }
9cf90a4… drh 2429
9cf90a4… drh 2430 /*
9cf90a4… drh 2431 ** z points to someplace in the middle of HTML markup. Return the length
9cf90a4… drh 2432 ** of the subtoken that starts on z.
9cf90a4… drh 2433 */
9cf90a4… drh 2434 int html_subtoken_length(const char *z){
9cf90a4… drh 2435 int n;
9cf90a4… drh 2436 char c;
9cf90a4… drh 2437 c = z[0];
9cf90a4… drh 2438 if( fossil_isspace(c) ){
9cf90a4… drh 2439 for(n=1; z[n] && fossil_isspace(z[n]); n++){}
9cf90a4… drh 2440 return n;
9cf90a4… drh 2441 }
9cf90a4… drh 2442 if( c=='"' || c=='\'' ){
9cf90a4… drh 2443 for(n=1; z[n] && z[n]!=c && z[n]!='>'; n++){}
9cf90a4… drh 2444 if( z[n]==c ) n++;
9cf90a4… drh 2445 return n;
9cf90a4… drh 2446 }
9cf90a4… drh 2447 if( c=='>' ){
9cf90a4… drh 2448 return 0;
9cf90a4… drh 2449 }
9cf90a4… drh 2450 if( c=='=' ){
9cf90a4… drh 2451 return 1;
9cf90a4… drh 2452 }
9cf90a4… drh 2453 if( fossil_isalnum(c) || c=='/' ){
9cf90a4… drh 2454 for(n=1; (c=z[n])!=0 && (fossil_isalnum(c) || c=='-' || c=='_'); n++){}
9cf90a4… drh 2455 return n;
9cf90a4… drh 2456 }
9cf90a4… drh 2457 return 1;
9cf90a4… drh 2458 }
9cf90a4… drh 2459
9cf90a4… drh 2460 /*
9cf90a4… drh 2461 ** z points to an HTML markup token: <TAG ATTR=VALUE ...>
9cf90a4… drh 2462 ** This routine looks for the VALUE associated with zAttr and returns
9cf90a4… drh 2463 ** a pointer to the start of that value and sets *pLen to be the length
9cf90a4… drh 2464 ** in bytes for the value. Or it returns NULL if no such attr exists.
9cf90a4… drh 2465 */
9cf90a4… drh 2466 const char *html_attribute(const char *zMarkup, const char *zAttr, int *pLen){
9cf90a4… drh 2467 int i = 1;
9cf90a4… drh 2468 int n;
9cf90a4… drh 2469 int nAttr;
9cf90a4… drh 2470 int iMatchCnt = 0;
9cf90a4… drh 2471 assert( zMarkup[0]=='<' );
9cf90a4… drh 2472 assert( zMarkup[1]!=0 );
9cf90a4… drh 2473 n = html_subtoken_length(zMarkup+i);
9cf90a4… drh 2474 if( n==0 ) return 0;
9cf90a4… drh 2475 i += n;
9cf90a4… drh 2476 nAttr = (int)strlen(zAttr);
9cf90a4… drh 2477 while( 1 ){
9cf90a4… drh 2478 const char *zStart = zMarkup+i;
9cf90a4… drh 2479 n = html_subtoken_length(zStart);
9cf90a4… drh 2480 if( n==0 ) break;
9cf90a4… drh 2481 i += n;
9cf90a4… drh 2482 if( fossil_isspace(zStart[0]) ) continue;
9cf90a4… drh 2483 if( n==nAttr && fossil_strnicmp(zAttr,zStart,nAttr)==0 ){
9cf90a4… drh 2484 iMatchCnt = 1;
9cf90a4… drh 2485 }else if( n==1 && zStart[0]=='=' && iMatchCnt==1 ){
9cf90a4… drh 2486 iMatchCnt = 2;
9cf90a4… drh 2487 }else if( iMatchCnt==2 ){
9cf90a4… drh 2488 if( (zStart[0]=='"' || zStart[0]=='\'') && zStart[n-1]==zStart[0] ){
9cf90a4… drh 2489 zStart++;
9cf90a4… drh 2490 n -= 2;
275da70… danield 2491 }
9cf90a4… drh 2492 *pLen = n;
9cf90a4… drh 2493 return zStart;
9cf90a4… drh 2494 }else{
9cf90a4… drh 2495 iMatchCnt = 0;
9cf90a4… drh 2496 }
9cf90a4… drh 2497 }
9cf90a4… drh 2498 return 0;
9cf90a4… drh 2499 }
9cf90a4… drh 2500
9cf90a4… drh 2501 /*
9cf90a4… drh 2502 ** COMMAND: test-html-tokenize
9cf90a4… drh 2503 **
9cf90a4… drh 2504 ** Tokenize an HTML file. Return the offset and length and text of
9cf90a4… drh 2505 ** each token - one token per line. Omit white-space tokens.
9cf90a4… drh 2506 */
9cf90a4… drh 2507 void test_html_tokenize(void){
9cf90a4… drh 2508 Blob in;
9cf90a4… drh 2509 char *z;
9cf90a4… drh 2510 int i;
9cf90a4… drh 2511 int iOfst, n;
9cf90a4… drh 2512
9cf90a4… drh 2513 for(i=2; i<g.argc; i++){
9cf90a4… drh 2514 blob_read_from_file(&in, g.argv[i], ExtFILE);
9cf90a4… drh 2515 z = blob_str(&in);
9cf90a4… drh 2516 for(iOfst=0; z[iOfst]; iOfst+=n){
9cf90a4… drh 2517 n = html_token_length(z+iOfst);
9cf90a4… drh 2518 if( fossil_isspace(z[iOfst]) ) continue;
9cf90a4… drh 2519 fossil_print("%d %d %.*s\n", iOfst, n, n, z+iOfst);
9cf90a4… drh 2520 if( z[iOfst]=='<' && n>1 ){
9cf90a4… drh 2521 int j,k;
9cf90a4… drh 2522 for(j=iOfst+1; (k = html_subtoken_length(z+j))>0; j+=k){
9cf90a4… drh 2523 if( fossil_isspace(z[j]) || z[j]=='=' ) continue;
9cf90a4… drh 2524 fossil_print("# %d %d %.*s\n", j, k, k, z+j);
9cf90a4… drh 2525 }
9cf90a4… drh 2526 }
9cf90a4… drh 2527 }
9cf90a4… drh 2528 blob_reset(&in);
9cf90a4… drh 2529 }
65870e8… drh 2530 }
65870e8… drh 2531
65870e8… drh 2532 /*
65870e8… drh 2533 ** Attempt to reformat messy HTML to be easily readable by humans.
65870e8… drh 2534 **
65870e8… drh 2535 ** * Try to keep lines less than 80 characters in length
65870e8… drh 2536 ** * Collapse white space into a single space
65870e8… drh 2537 ** * Put a blank line before:
65870e8… drh 2538 ** <blockquote><center><code><hN><p><pre><table>
65870e8… drh 2539 ** * Put a newline after <br> and <hr>
65870e8… drh 2540 ** * Start each of the following elements on a new line:
65870e8… drh 2541 ** <address><cite><dd><div><dl><dt><li><ol><samp>
65870e8… drh 2542 ** <tbody><td><tfoot><th><thead><tr><ul>
65870e8… drh 2543 **
65870e8… drh 2544 ** Except, do not do any reformatting inside of <pre>...</pre>
65870e8… drh 2545 */
65870e8… drh 2546 void htmlTidy(const char *zIn, Blob *pOut){
65870e8… drh 2547 int n;
65870e8… drh 2548 int nPre = 0;
65870e8… drh 2549 int iCur = 0;
65870e8… drh 2550 int wantSpace = 0;
65870e8… drh 2551 int omitSpace = 1;
65870e8… drh 2552 while( zIn[0] ){
9cf90a4… drh 2553 n = html_token_length(zIn);
65870e8… drh 2554 if( zIn[0]=='<' && n>1 ){
65870e8… drh 2555 int i, j;
65870e8… drh 2556 int isCloseTag;
65870e8… drh 2557 int eTag;
65870e8… drh 2558 int eType;
65870e8… drh 2559 char zTag[32];
65870e8… drh 2560 isCloseTag = zIn[1]=='/';
65870e8… drh 2561 for(i=0, j=1+isCloseTag; i<30 && fossil_isalnum(zIn[j]); i++, j++){
65870e8… drh 2562 zTag[i] = fossil_tolower(zIn[j]);
65870e8… drh 2563 }
65870e8… drh 2564 zTag[i] = 0;
65870e8… drh 2565 eTag = findTag(zTag);
65870e8… drh 2566 eType = aMarkup[eTag].iType;
65870e8… drh 2567 if( eTag==MARKUP_PRE ){
65870e8… drh 2568 if( isCloseTag ){
65870e8… drh 2569 nPre--;
65870e8… drh 2570 blob_append(pOut, zIn, n);
65870e8… drh 2571 zIn += n;
48c47e1… drh 2572 if( nPre==0 ){ blob_append_char(pOut, '\n'); iCur = 0; }
65870e8… drh 2573 continue;
65870e8… drh 2574 }else{
48c47e1… drh 2575 if( iCur && nPre==0 ){ blob_append_char(pOut, '\n'); iCur = 0; }
65870e8… drh 2576 nPre++;
65870e8… drh 2577 }
65870e8… drh 2578 }else if( eType & (MUTYPE_BLOCK|MUTYPE_TABLE) ){
65870e8… drh 2579 if( !isCloseTag && nPre==0 && blob_size(pOut)>0 ){
65870e8… drh 2580 blob_append(pOut, "\n\n", 1 + (iCur>0));
65870e8… drh 2581 iCur = 0;
65870e8… drh 2582 }
65870e8… drh 2583 wantSpace = 0;
65870e8… drh 2584 omitSpace = 1;
65870e8… drh 2585 }else if( (eType & (MUTYPE_LIST|MUTYPE_LI|MUTYPE_TR|MUTYPE_TD))!=0
65870e8… drh 2586 || eTag==MARKUP_HR
65870e8… drh 2587 ){
65870e8… drh 2588 if( nPre==0 && (!isCloseTag || (eType&MUTYPE_LIST)!=0) && iCur>0 ){
48c47e1… drh 2589 blob_append_char(pOut, '\n');
65870e8… drh 2590 iCur = 0;
65870e8… drh 2591 }
65870e8… drh 2592 wantSpace = 0;
65870e8… drh 2593 omitSpace = 1;
65870e8… drh 2594 }
65870e8… drh 2595 if( wantSpace && nPre==0 ){
65870e8… drh 2596 if( iCur+n+1>=80 ){
48c47e1… drh 2597 blob_append_char(pOut, '\n');
65870e8… drh 2598 iCur = 0;
65870e8… drh 2599 }else{
48c47e1… drh 2600 blob_append_char(pOut, ' ');
65870e8… drh 2601 iCur++;
65870e8… drh 2602 }
65870e8… drh 2603 }
65870e8… drh 2604 blob_append(pOut, zIn, n);
65870e8… drh 2605 iCur += n;
65870e8… drh 2606 wantSpace = 0;
65870e8… drh 2607 if( eTag==MARKUP_BR || eTag==MARKUP_HR ){
48c47e1… drh 2608 blob_append_char(pOut, '\n');
65870e8… drh 2609 iCur = 0;
65870e8… drh 2610 }
65870e8… drh 2611 }else if( fossil_isspace(zIn[0]) ){
65870e8… drh 2612 if( nPre ){
65870e8… drh 2613 blob_append(pOut, zIn, n);
65870e8… drh 2614 }else{
65870e8… drh 2615 wantSpace = !omitSpace;
65870e8… drh 2616 }
65870e8… drh 2617 }else{
65870e8… drh 2618 if( wantSpace && nPre==0 ){
65870e8… drh 2619 if( iCur+n+1>=80 ){
48c47e1… drh 2620 blob_append_char(pOut, '\n');
65870e8… drh 2621 iCur = 0;
65870e8… drh 2622 }else{
48c47e1… drh 2623 blob_append_char(pOut, ' ');
65870e8… drh 2624 iCur++;
65870e8… drh 2625 }
65870e8… drh 2626 }
65870e8… drh 2627 blob_append(pOut, zIn, n);
65870e8… drh 2628 iCur += n;
65870e8… drh 2629 wantSpace = omitSpace = 0;
65870e8… drh 2630 }
65870e8… drh 2631 zIn += n;
65870e8… drh 2632 }
48c47e1… drh 2633 if( iCur ) blob_append_char(pOut, '\n');
65870e8… drh 2634 }
65870e8… drh 2635
65870e8… drh 2636 /*
65870e8… drh 2637 ** COMMAND: test-html-tidy
7ab0328… drh 2638 **
7ab0328… drh 2639 ** Run the htmlTidy() routine on the content of all files named on
7ab0328… drh 2640 ** the command-line and write the results to standard output.
65870e8… drh 2641 */
65870e8… drh 2642 void test_html_tidy(void){
65870e8… drh 2643 Blob in, out;
65870e8… drh 2644 int i;
65870e8… drh 2645
65870e8… drh 2646 for(i=2; i<g.argc; i++){
1772357… drh 2647 blob_read_from_file(&in, g.argv[i], ExtFILE);
65870e8… drh 2648 blob_zero(&out);
65870e8… drh 2649 htmlTidy(blob_str(&in), &out);
65870e8… drh 2650 blob_reset(&in);
3a56132… drh 2651 fossil_puts(blob_buffer(&out), 0, blob_size(&out));
cbd8e67… drh 2652 blob_reset(&out);
cbd8e67… drh 2653 }
3a56132… drh 2654 }
3a56132… drh 2655
fd59eb1… drh 2656 #if INTERFACE
fd59eb1… drh 2657 /*
fd59eb1… drh 2658 ** Allowed flag options for html_to_plaintext().
fd59eb1… drh 2659 */
d62ca2b… drh 2660 #define HTOT_VT100 0x01 /* <mark> becomes ^[[91m */
d62ca2b… drh 2661 #define HTOT_FLOW 0x02 /* Collapse internal whitespace to a single space */
d62ca2b… drh 2662 #define HTOT_TRIM 0x04 /* Trim off leading and trailing whitespace */
fd59eb1… drh 2663
fd59eb1… drh 2664 #endif /* INTERFACE */
a980566… drh 2665
a980566… drh 2666 /*
a980566… drh 2667 ** Add <MARK> or </MARK> to the output, or similar VT-100 escape
a980566… drh 2668 ** codes.
a980566… drh 2669 */
a980566… drh 2670 static void addMark(Blob *pOut, int mFlags, int isClose){
a980566… drh 2671 static const char *az[4] = { "<MARK>", "</MARK>", "\033[91m", "\033[0m" };
a980566… drh 2672 int i = 0;
a980566… drh 2673 if( isClose ) i++;
a980566… drh 2674 if( mFlags & HTOT_VT100 ) i += 2;
a980566… drh 2675 blob_append(pOut, az[i], -1);
a980566… drh 2676 }
fd59eb1… drh 2677
cbd8e67… drh 2678 /*
cbd8e67… drh 2679 ** Remove all HTML markup from the input text. The output written into
cbd8e67… drh 2680 ** pOut is pure text.
0f96ffb… drh 2681 **
0f96ffb… drh 2682 ** Put the title on the first line, if there is any <title> markup.
0f96ffb… drh 2683 ** If there is no <title>, then create a blank first line.
cbd8e67… drh 2684 */
fd59eb1… drh 2685 void html_to_plaintext(const char *zIn, Blob *pOut, int mFlags){
cbd8e67… drh 2686 int n;
cbd8e67… drh 2687 int i, j;
d62ca2b… drh 2688 int bFlow = 0; /* Transform internal WS into a single space */
d62ca2b… drh 2689 int prevWS = 1; /* Previous output was whitespace or start of msg */
d62ca2b… drh 2690 int nMark = 0; /* True if inside of <mark>..</mark> */
d62ca2b… drh 2691
d62ca2b… drh 2692 for(i=0; fossil_isspace(zIn[i]); i++){}
d62ca2b… drh 2693 if( i>0 && (mFlags & HTOT_TRIM)==0 ){
d62ca2b… drh 2694 blob_append(pOut, zIn, i);
d62ca2b… drh 2695 }
d62ca2b… drh 2696 zIn += i;
d62ca2b… drh 2697 if( mFlags & HTOT_FLOW ) bFlow = 1;
cbd8e67… drh 2698 while( zIn[0] ){
9cf90a4… drh 2699 n = html_token_length(zIn);
cbd8e67… drh 2700 if( zIn[0]=='<' && n>1 ){
cbd8e67… drh 2701 int isCloseTag;
cbd8e67… drh 2702 int eTag;
cbd8e67… drh 2703 int eType;
cbd8e67… drh 2704 char zTag[32];
d62ca2b… drh 2705 prevWS = 0;
cbd8e67… drh 2706 isCloseTag = zIn[1]=='/';
cbd8e67… drh 2707 for(i=0, j=1+isCloseTag; i<30 && fossil_isalnum(zIn[j]); i++, j++){
cbd8e67… drh 2708 zTag[i] = fossil_tolower(zIn[j]);
cbd8e67… drh 2709 }
cbd8e67… drh 2710 zTag[i] = 0;
cbd8e67… drh 2711 eTag = findTag(zTag);
cbd8e67… drh 2712 eType = aMarkup[eTag].iType;
cbd8e67… drh 2713 if( eTag==MARKUP_INVALID && fossil_strnicmp(zIn,"<style",6)==0 ){
cbd8e67… drh 2714 zIn += n;
cbd8e67… drh 2715 while( zIn[0] ){
9cf90a4… drh 2716 n = html_token_length(zIn);
cbd8e67… drh 2717 if( fossil_strnicmp(zIn, "</style",7)==0 ) break;
cbd8e67… drh 2718 zIn += n;
cbd8e67… drh 2719 }
cbd8e67… drh 2720 if( zIn[0]=='<' ) zIn += n;
cbd8e67… drh 2721 continue;
cbd8e67… drh 2722 }
fd59eb1… drh 2723 if( eTag==MARKUP_INVALID && strcmp(zTag,"mark")==0 ){
a980566… drh 2724 if( isCloseTag && nMark ){
a980566… drh 2725 addMark(pOut, mFlags, 1);
a980566… drh 2726 nMark = 0;
a980566… drh 2727 }else if( !isCloseTag && !nMark ){
a980566… drh 2728 addMark(pOut, mFlags, 0);
a980566… drh 2729 nMark = 1;
fd59eb1… drh 2730 }
fd59eb1… drh 2731 zIn += n;
fd59eb1… drh 2732 continue;
fd59eb1… drh 2733 }
fd59eb1… drh 2734 if( eTag==MARKUP_TITLE ){
d62ca2b… drh 2735 if( isCloseTag && (mFlags & HTOT_FLOW)==0 ){
d62ca2b… drh 2736 bFlow = 0;
d62ca2b… drh 2737 }else{
d62ca2b… drh 2738 bFlow = 1;
d62ca2b… drh 2739 }
d62ca2b… drh 2740 }
d62ca2b… drh 2741 if( !isCloseTag && (eType & (MUTYPE_BLOCK|MUTYPE_TABLE))!=0 ){
d62ca2b… drh 2742 blob_append_char(pOut, '\n');
d62ca2b… drh 2743 }
d62ca2b… drh 2744 }else if( fossil_isspace(zIn[0]) ){
d62ca2b… drh 2745 if( bFlow==0 ){
d62ca2b… drh 2746 if( zIn[n]==0 && (mFlags & HTOT_TRIM) ) break;
d62ca2b… drh 2747 blob_append(pOut, zIn, n);
d62ca2b… drh 2748 }else if( !prevWS ){
d62ca2b… drh 2749 prevWS = 1;
d62ca2b… drh 2750 blob_append_char(pOut, ' ');
d62ca2b… drh 2751 zIn += n;
d62ca2b… drh 2752 n = 0;
4ee3362… drh 2753 }
4ee3362… drh 2754 }else if( zIn[0]=='&' ){
4ee3362… drh 2755 u32 c = '?';
d62ca2b… drh 2756 prevWS = 0;
4ee3362… drh 2757 if( zIn[1]=='#' ){
4ee3362… drh 2758 c = atoi(&zIn[2]);
4ee3362… drh 2759 if( c==0 ) c = '?';
4ee3362… drh 2760 }else{
4ee3362… drh 2761 static const struct { int n; u32 c; char *z; } aEntity[] = {
0f96ffb… drh 2762 { 5, '&', "&amp;" },
0f96ffb… drh 2763 { 4, '<', "&lt;" },
0f96ffb… drh 2764 { 4, '>', "&gt;" },
0f96ffb… drh 2765 { 6, ' ', "&nbsp;" },
4ee3362… drh 2766 { 6, '"', "&quot;" },
0f96ffb… drh 2767 };
0f96ffb… drh 2768 int jj;
3cb9ba4… andygoth 2769 for(jj=0; jj<count(aEntity); jj++){
0f96ffb… drh 2770 if( aEntity[jj].n==n && strncmp(aEntity[jj].z,zIn,n)==0 ){
0f96ffb… drh 2771 c = aEntity[jj].c;
0f96ffb… drh 2772 break;
0f96ffb… drh 2773 }
0f96ffb… drh 2774 }
0f96ffb… drh 2775 }
d62ca2b… drh 2776 if( c<0x00080 ){
d62ca2b… drh 2777 blob_append_char(pOut, c & 0xff);
d62ca2b… drh 2778 }else if( c<0x00800 ){
d62ca2b… drh 2779 blob_append_char(pOut, 0xc0 + (u8)((c>>6)&0x1f));
d62ca2b… drh 2780 blob_append_char(pOut, 0x80 + (u8)(c&0x3f));
d62ca2b… drh 2781 }else if( c<0x10000 ){
d62ca2b… drh 2782 blob_append_char(pOut, 0xe0 + (u8)((c>>12)&0x0f));
d62ca2b… drh 2783 blob_append_char(pOut, 0x80 + (u8)((c>>6)&0x3f));
d62ca2b… drh 2784 blob_append_char(pOut, 0x80 + (u8)(c&0x3f));
d62ca2b… drh 2785 }else{
d62ca2b… drh 2786 blob_append_char(pOut, 0xf0 + (u8)((c>>18)&0x07));
d62ca2b… drh 2787 blob_append_char(pOut, 0x80 + (u8)((c>>12)&0x3f));
d62ca2b… drh 2788 blob_append_char(pOut, 0x80 + (u8)((c>>6)&0x3f));
d62ca2b… drh 2789 blob_append_char(pOut, 0x80 + (u8)(c&0x3f));
d62ca2b… drh 2790 }
d62ca2b… drh 2791 }else{
d62ca2b… drh 2792 prevWS = 0;
cbd8e67… drh 2793 blob_append(pOut, zIn, n);
cbd8e67… drh 2794 }
cbd8e67… drh 2795 zIn += n;
cbd8e67… drh 2796 }
d62ca2b… drh 2797 if( nMark ){
d62ca2b… drh 2798 addMark(pOut, mFlags, 1);
d62ca2b… drh 2799 }
cbd8e67… drh 2800 }
cbd8e67… drh 2801
cbd8e67… drh 2802 /*
cbd8e67… drh 2803 ** COMMAND: test-html-to-text
7ab0328… drh 2804 **
fd59eb1… drh 2805 ** Usage: %fossil test-html-to-text [OPTIONS] FILE ...
7ab0328… drh 2806 **
7ab0328… drh 2807 ** Read all files named on the command-line. Convert the file
7ab0328… drh 2808 ** content from HTML to text and write the results on standard
7ab0328… drh 2809 ** output.
7ab0328… drh 2810 **
7ab0328… drh 2811 ** This command is intended as a test and debug interface for
7ab0328… drh 2812 ** the html_to_plaintext() routine.
fd59eb1… drh 2813 **
fd59eb1… drh 2814 ** Options:
fd59eb1… drh 2815 **
fd59eb1… drh 2816 ** --vt100 Translate <mark> and </mark> into ANSI/VT100
fd59eb1… drh 2817 ** escapes to highlight the contained text.
cbd8e67… drh 2818 */
cbd8e67… drh 2819 void test_html_to_text(void){
cbd8e67… drh 2820 Blob in, out;
cbd8e67… drh 2821 int i;
fd59eb1… drh 2822 int mFlags = 0;
fd59eb1… drh 2823 if( find_option("vt100",0,0)!=0 ) mFlags |= HTOT_VT100;
cbd8e67… drh 2824
cbd8e67… drh 2825 for(i=2; i<g.argc; i++){
1772357… drh 2826 blob_read_from_file(&in, g.argv[i], ExtFILE);
cbd8e67… drh 2827 blob_zero(&out);
fd59eb1… drh 2828 html_to_plaintext(blob_str(&in), &out, mFlags);
cbd8e67… drh 2829 blob_reset(&in);
3a56132… drh 2830 fossil_puts(blob_buffer(&out), 0, blob_size(&out));
65870e8… drh 2831 blob_reset(&out);
aadf917… drh 2832 }
aadf917… drh 2833 }
aadf917… drh 2834
54c1fd6… drh 2835 /****************************************************************************
54c1fd6… drh 2836 ** safe-html:
54c1fd6… drh 2837 **
54c1fd6… drh 2838 ** An interface for preventing HTML constructs (ex: <style>, <form>, etc)
54c1fd6… drh 2839 ** from being inserted into Wiki and Forum posts using Markdown. See the
54c1fd6… drh 2840 ** comment on safe_html_append() for additional information on what is meant
54c1fd6… drh 2841 ** by "safe".
54c1fd6… drh 2842 **
54c1fd6… drh 2843 ** The safe-html restrictions only apply to Markdown, as Fossil-Wiki only
54c1fd6… drh 2844 ** allows safe-html by design - unsafe-HTML is never and has never been
54c1fd6… drh 2845 ** allowed in Fossil-Wiki.
54c1fd6… drh 2846 **
54c1fd6… drh 2847 ** This code is in the wikiformat.c file so that it can have access to the
54c1fd6… drh 2848 ** white-list of acceptable HTML in the aMarkup[] array.
54c1fd6… drh 2849 */
54c1fd6… drh 2850
aadf917… drh 2851 /*
aadf917… drh 2852 ** An instance of this object keeps track of the nesting of HTML
54c1fd6… drh 2853 ** elements for safe_html_append().
aadf917… drh 2854 */
54c1fd6… drh 2855 typedef struct HtmlTagStack HtmlTagStack;
aadf917… drh 2856 struct HtmlTagStack {
aadf917… drh 2857 int n; /* Current tag stack depth */
aadf917… drh 2858 int nAlloc; /* Space allocated for aStack[] */
aadf917… drh 2859 int *aStack; /* The stack of tags */
aadf917… drh 2860 int aSpace[10]; /* Initial static space, to avoid malloc() */
aadf917… drh 2861 };
aadf917… drh 2862
aadf917… drh 2863 /*
aadf917… drh 2864 ** Initialize bulk memory to a valid empty tagstack.
aadf917… drh 2865 */
54c1fd6… drh 2866 static void html_tagstack_init(HtmlTagStack *p){
aadf917… drh 2867 p->n = 0;
aadf917… drh 2868 p->nAlloc = 0;
aadf917… drh 2869 p->aStack = p->aSpace;
aadf917… drh 2870 }
aadf917… drh 2871
aadf917… drh 2872 /*
e2bdc10… danield 2873 ** Push a new element onto the tag stack
aadf917… drh 2874 */
54c1fd6… drh 2875 static void html_tagstack_push(HtmlTagStack *p, int e){
aadf917… drh 2876 if( p->n>=ArraySize(p->aSpace) && p->n>=p->nAlloc ){
aadf917… drh 2877 if( p->nAlloc==0 ){
aadf917… drh 2878 int *aNew;
aadf917… drh 2879 p->nAlloc = 50;
aadf917… drh 2880 aNew = fossil_malloc( sizeof(p->aStack[0])*p->nAlloc );
aadf917… drh 2881 memcpy(aNew, p->aStack, sizeof(p->aStack[0])*p->n );
aadf917… drh 2882 p->aStack = aNew;
aadf917… drh 2883 }else{
aadf917… drh 2884 p->nAlloc *= 2;
aadf917… drh 2885 p->aStack = fossil_realloc(p->aStack, sizeof(p->aStack[0])*p->nAlloc );
aadf917… drh 2886 }
aadf917… drh 2887 }
aadf917… drh 2888 p->aStack[p->n++] = e;
aadf917… drh 2889 }
aadf917… drh 2890
aadf917… drh 2891 /*
aadf917… drh 2892 ** Clear a tag stack, reclaiming any memory allocations.
aadf917… drh 2893 */
54c1fd6… drh 2894 static void html_tagstack_clear(HtmlTagStack *p){
aadf917… drh 2895 if( p->nAlloc ){
aadf917… drh 2896 fossil_free(p->aStack);
aadf917… drh 2897 p->nAlloc = 0;
aadf917… drh 2898 p->aStack = p->aSpace;
aadf917… drh 2899 }
aadf917… drh 2900 p->n = 0;
aadf917… drh 2901 }
aadf917… drh 2902
aadf917… drh 2903 /*
aadf917… drh 2904 ** The HTML end-tag eEnd wants to be added to pBlob.
aadf917… drh 2905 **
aadf917… drh 2906 ** If an open-tag for eEnd exists anywhere on the stack, then
aadf917… drh 2907 ** pop it and all prior elements from the task, issuing appropriate
aadf917… drh 2908 ** end-tags as you go.
aadf917… drh 2909 **
aadf917… drh 2910 ** If there is no open-tag for eEnd on the stack, then this
aadf917… drh 2911 ** routine is a no-op.
aadf917… drh 2912 */
54c1fd6… drh 2913 static void html_tagstack_pop(HtmlTagStack *p, Blob *pBlob, int eEnd){
a7ebb8d… drh 2914 int i, e;
a7ebb8d… drh 2915 if( eEnd!=0 ){
a7ebb8d… drh 2916 for(i=p->n-1; i>=0 && p->aStack[i]!=eEnd; i--){}
a7ebb8d… drh 2917 if( i<0 ){
a7ebb8d… drh 2918 blob_appendf(pBlob, "<span class='error'>&lt;/%s&gt;</span>",
a7ebb8d… drh 2919 aMarkup[eEnd].zName);
a7ebb8d… drh 2920 return;
a7ebb8d… drh 2921 }
a7ebb8d… drh 2922 }else if( p->n==0 ){
bc2e6fb… drh 2923 return;
bc2e6fb… drh 2924 }
aadf917… drh 2925 do{
a7ebb8d… drh 2926 e = p->aStack[--p->n];
a7ebb8d… drh 2927 if( e==eEnd || (aMarkup[e].iType & MUTYPE_Nested)!=0 ){
a7ebb8d… drh 2928 blob_appendf(pBlob, "</%s>", aMarkup[e].zName);
a7ebb8d… drh 2929 }
a7ebb8d… drh 2930 }while( e!=eEnd && p->n>0 );
a7ebb8d… drh 2931 }
a7ebb8d… drh 2932
a7ebb8d… drh 2933 /*
dfa0eee… drh 2934 ** Return a nonce to indicate that safe_html() can allow code through
dfa0eee… drh 2935 ** without censoring.
dfa0eee… drh 2936 **
dfa0eee… drh 2937 ** When safe_html() is asked to sanitize some HTML, it will ignore
dfa0eee… drh 2938 ** any text in between two consecutive instances of the nonce. The
dfa0eee… drh 2939 ** nonce itself is an HTML comment so it is harmless to keep the
dfa0eee… drh 2940 ** nonce in the middle of the HTML stream. A different nonce is
e2bdc10… danield 2941 ** chosen each time Fossil is run, using a lot of randomness, so
dfa0eee… drh 2942 ** an attacker will be unable to guess the nonce in advance.
dfa0eee… drh 2943 **
dfa0eee… drh 2944 ** The original use-case for this mechanism is to allow Pikchr-generated
dfa0eee… drh 2945 ** SVG in the middle of HTML generated from Markdown. The Markdown
dfa0eee… drh 2946 ** output will normally be processed by safe_html() to prevent accidental
dfa0eee… drh 2947 ** or malicious introduction of harmful HTML (ex: <script>) in the
dfa0eee… drh 2948 ** output stream. The safe_html() only lets through HTML elements
dfa0eee… drh 2949 ** that are on its allow-list and SVG is not on that list. Hence, in order
dfa0eee… drh 2950 ** to allow the Pikchr-generated SVG through, it must be surrounded by
dfa0eee… drh 2951 ** the nonce.
a13082c… drh 2952 */
a13082c… drh 2953 const char *safe_html_nonce(int bGenerate){
a13082c… drh 2954 static char *zNonce = 0;
a13082c… drh 2955 if( zNonce==0 && bGenerate ){
a13082c… drh 2956 zNonce = db_text(0, "SELECT '<!--'||hex(randomblob(32))||'-->';");
a13082c… drh 2957 }
a13082c… drh 2958 return zNonce;
a13082c… drh 2959 }
a13082c… drh 2960 #define SAFE_NONCE_SIZE (4+64+3)
a13082c… drh 2961
a13082c… drh 2962 /*
03ce4e7… drh 2963 ** Append a safe translation of HTML text to a Blob object.
03ce4e7… drh 2964 **
03ce4e7… drh 2965 ** Restriction: The input to this routine must be writable.
f4e6efb… drh 2966 * Temporary changes may be made to the input, but the input is restored
f4e6efb… drh 2967 ** to its original state prior to returning. If zHtml[nHtml] is not a
f4e6efb… drh 2968 ** zero character, then a zero might be written in that position
f4e6efb… drh 2969 ** temporarily, but that slot will also be restored before this routine
f4e6efb… drh 2970 ** returns.
f4e6efb… drh 2971 */
03ce4e7… drh 2972 static void safe_html_append(Blob *pBlob, char *zHtml, int nHtml){
aadf917… drh 2973 char cLast;
aadf917… drh 2974 int i, j, n;
aadf917… drh 2975 HtmlTagStack s;
aadf917… drh 2976 ParsedMarkup markup;
a13082c… drh 2977 const char *zNonce;
a13082c… drh 2978 char *z;
cd0b228… drh 2979
cd0b228… drh 2980 if( nHtml<=0 ) return;
aadf917… drh 2981 cLast = zHtml[nHtml];
aadf917… drh 2982 zHtml[nHtml] = 0;
aadf917… drh 2983 html_tagstack_init(&s);
aadf917… drh 2984
aadf917… drh 2985 i = 0;
aadf917… drh 2986 while( i<nHtml ){
aadf917… drh 2987 if( zHtml[i]=='<' ){
aadf917… drh 2988 j = i;
aadf917… drh 2989 }else{
a13082c… drh 2990 z = strchr(zHtml+i, '<');
aadf917… drh 2991 if( z==0 ){
aadf917… drh 2992 blob_append(pBlob, zHtml+i, nHtml-i);
aadf917… drh 2993 break;
aadf917… drh 2994 }
aadf917… drh 2995 j = (int)(z - zHtml);
aadf917… drh 2996 blob_append(pBlob, zHtml+i, j-i);
a13082c… drh 2997 }
a13082c… drh 2998 if( zHtml[j+1]=='!'
a13082c… drh 2999 && j+2*SAFE_NONCE_SIZE<nHtml
a13082c… drh 3000 && (zNonce = safe_html_nonce(0))!=0
a13082c… drh 3001 && strncmp(zHtml+j,zNonce,SAFE_NONCE_SIZE)==0
a13082c… drh 3002 && (z = strstr(zHtml+j+SAFE_NONCE_SIZE,zNonce))!=0
a13082c… drh 3003 ){
a13082c… drh 3004 i = (int)(z - zHtml) + SAFE_NONCE_SIZE;
a13082c… drh 3005 blob_append(pBlob, zHtml+j, i-j);
a13082c… drh 3006 continue;
aadf917… drh 3007 }
aadf917… drh 3008 n = html_tag_length(zHtml+j);
aadf917… drh 3009 if( n==0 ){
aadf917… drh 3010 blob_append(pBlob, "&lt;", 4);
aadf917… drh 3011 i = j+1;
aadf917… drh 3012 continue;
aadf917… drh 3013 }else{
aadf917… drh 3014 i = j + n;
aadf917… drh 3015 }
aadf917… drh 3016 parseMarkup(&markup, zHtml+j);
bc2e6fb… drh 3017 if( markup.iCode==MARKUP_INVALID ){
16d68b0… drh 3018 unparseMarkup(&markup);
bc2e6fb… drh 3019 blob_appendf(pBlob, "<span class='error'>&lt;%.*s&gt;</span>",
bc2e6fb… drh 3020 n-2, zHtml+j+1);
16d68b0… drh 3021 continue;
16d68b0… drh 3022 }
16d68b0… drh 3023 if( (markup.iType & MUTYPE_Nested)==0 || markup.iCode==MARKUP_P ){
382f373… drh 3024 renderMarkup(pBlob, &markup);
bc2e6fb… drh 3025 }else{
aadf917… drh 3026 if( markup.endTag ){
aadf917… drh 3027 html_tagstack_pop(&s, pBlob, markup.iCode);
aadf917… drh 3028 }else{
aadf917… drh 3029 renderMarkup(pBlob, &markup);
a7807a8… drh 3030 html_tagstack_push(&s, markup.iCode);
aadf917… drh 3031 }
aadf917… drh 3032 }
aadf917… drh 3033 unparseMarkup(&markup);
aadf917… drh 3034 }
a7ebb8d… drh 3035 html_tagstack_pop(&s, pBlob, 0);
aadf917… drh 3036 html_tagstack_clear(&s);
aadf917… drh 3037 zHtml[nHtml] = cLast;
aadf917… drh 3038 }
aadf917… drh 3039
382f373… drh 3040 /*
89b6dda… drh 3041 ** This local variable is true if the safe_html() function is enabled.
89b6dda… drh 3042 ** In other words, this is true if the output of Markdown should be
89b6dda… drh 3043 ** restricted to use only "safe" HTML.
89b6dda… drh 3044 */
89b6dda… drh 3045 static int safeHtmlEnable = 1;
89b6dda… drh 3046
89b6dda… drh 3047
89b6dda… drh 3048 #if INTERFACE
89b6dda… drh 3049 /*
89b6dda… drh 3050 ** Allowed values for the eTrust parameter to safe_html_context().
89b6dda… drh 3051 */
89b6dda… drh 3052 #define DOCSRC_FILE 1 /* Document is a checked-in file */
89b6dda… drh 3053 #define DOCSRC_FORUM 2 /* Document is a forum post */
89b6dda… drh 3054 #define DOCSRC_TICKET 3 /* Document is a ticket comment */
89b6dda… drh 3055 #define DOCSRC_WIKI 4 /* Document is a wiki page */
89b6dda… drh 3056 #define DOCSRC_TRUSTED 5 /* safe_html() is always a no-op */
89b6dda… drh 3057 #define DOCSRC_UNTRUSTED 6 /* safe_html() is always enabled */
89b6dda… drh 3058 #endif /* INTERFACE */
89b6dda… drh 3059
89b6dda… drh 3060
89b6dda… drh 3061 /*
89b6dda… drh 3062 ** Specify the context in which a markdown document with potentially
89b6dda… drh 3063 ** unsafe HTML will be rendered.
89b6dda… drh 3064 */
89b6dda… drh 3065 void safe_html_context(int eTrust){
89b6dda… drh 3066 static const char *zSafeHtmlSetting = 0;
89b6dda… drh 3067 char cPerm = 0;
89b6dda… drh 3068 if( eTrust==DOCSRC_TRUSTED ){
89b6dda… drh 3069 safeHtmlEnable = 0;
89b6dda… drh 3070 return;
89b6dda… drh 3071 }
89b6dda… drh 3072 if( eTrust==DOCSRC_UNTRUSTED ){
89b6dda… drh 3073 safeHtmlEnable = 1;
89b6dda… drh 3074 return;
89b6dda… drh 3075 }
89b6dda… drh 3076 if( zSafeHtmlSetting==0 ){
89b6dda… drh 3077 zSafeHtmlSetting = db_get("safe-html", "");
89b6dda… drh 3078 }
89b6dda… drh 3079 switch( eTrust ){
89b6dda… drh 3080 case DOCSRC_FILE: cPerm = 'b'; break;
89b6dda… drh 3081 case DOCSRC_FORUM: cPerm = 'f'; break;
89b6dda… drh 3082 case DOCSRC_TICKET: cPerm = 't'; break;
89b6dda… drh 3083 case DOCSRC_WIKI: cPerm = 'w'; break;
89b6dda… drh 3084 }
89b6dda… drh 3085 safeHtmlEnable = (strchr(zSafeHtmlSetting,cPerm)==0);
89b6dda… drh 3086 }
89b6dda… drh 3087
89b6dda… drh 3088 /*
9ef2e5e… drh 3089 ** SETTING: safe-html width=8
9ef2e5e… drh 3090 ** This setting controls whether or not unsafe HTML elements
89e5ac1… stephan 3091 ** (such as SCRIPT or STYLE tags) are allowed in Markdown-formatted
9ef2e5e… drh 3092 ** documents. Unsafe HTML is disabled by default. If this setting
9ef2e5e… drh 3093 ** exists and is a string, then letters in that string can enable
9ef2e5e… drh 3094 ** unsafe HTML in various contexts:
9ef2e5e… drh 3095 **
89e5ac1… stephan 3096 ** - b Unsafe HTML allowed in embedded documentation
89e5ac1… stephan 3097 ** - f Unsafe HTML allowed in forum posts
89e5ac1… stephan 3098 ** - t Unsafe HTML allowed in tickets
89e5ac1… stephan 3099 ** - w Unsafe HTML allowed on wiki pages
9ef2e5e… drh 3100 */
9ef2e5e… drh 3101 /*
03ce4e7… drh 3102 ** The input blob contains HTML. If safe-html is enabled, then
03ce4e7… drh 3103 ** convert the input into "safe HTML". The following modifications
03ce4e7… drh 3104 ** are made:
03ce4e7… drh 3105 **
03ce4e7… drh 3106 ** 1. Remove any elements that are not on the AllowedMarkup list.
03ce4e7… drh 3107 ** (ex: <script>, <form>, etc.)
03ce4e7… drh 3108 **
03ce4e7… drh 3109 ** 2. Remove any attributes that are not on the AllowedMarkup list.
b473ba0… stephan 3110 ** (ex: onload=, etc.)
03ce4e7… drh 3111 **
03ce4e7… drh 3112 ** 3. Omit any surplus close-tags. This prevents the script from
03ce4e7… drh 3113 ** terminating an <div> or similar in the outer context.
03ce4e7… drh 3114 **
03ce4e7… drh 3115 ** 4. Insert additional close-tags as necessary so that any
03ce4e7… drh 3116 ** tag in the input that needs a close-tag has one. This
03ce4e7… drh 3117 ** prevents tags in the embedded script from affecting the
03ce4e7… drh 3118 ** display of content that follows this script in the enclosing
03ce4e7… drh 3119 ** context.
03ce4e7… drh 3120 **
b473ba0… stephan 3121 ** These modifications are intended to make the generated HTML safe
03ce4e7… drh 3122 ** to be embedded in a larger HTML document, such that the embedded
03ce4e7… drh 3123 ** HTML has no influence on the formatting and operation of the
03ce4e7… drh 3124 ** larger document.
03ce4e7… drh 3125 **
03ce4e7… drh 3126 ** If safe-html is disabled, then this routine is a no-op.
382f373… drh 3127 */
382f373… drh 3128 void safe_html(Blob *in){
03ce4e7… drh 3129 Blob out; /* Holding area for the revised text during construction */
03ce4e7… drh 3130 char *z; /* Original input text */
03ce4e7… drh 3131 int n; /* Number of bytes in the original input text */
382f373… drh 3132 int k;
03ce4e7… drh 3133
89b6dda… drh 3134 if( safeHtmlEnable==0 ) return;
03ce4e7… drh 3135 z = blob_str(in);
03ce4e7… drh 3136 n = blob_size(in);
382f373… drh 3137 blob_init(&out, 0, 0);
382f373… drh 3138 while( fossil_isspace(z[0]) ){ z++; n--; }
382f373… drh 3139 for(k=n-1; k>5 && fossil_isspace(z[k]); k--){}
382f373… drh 3140
382f373… drh 3141 if( fossil_strnicmp(z, "<div",4)==0 && !fossil_isalpha(z[4])
382f373… drh 3142 && fossil_strnicmp(z+k-5, "</div>",6)==0
382f373… drh 3143 ){
382f373… drh 3144 /* The input contains an outer <div>...</div>. Preserve the
382f373… drh 3145 ** full scope of that <div>. */
382f373… drh 3146 int m = html_tag_length(z);
382f373… drh 3147 k -= 5;
382f373… drh 3148 blob_append(&out, z, m);
382f373… drh 3149 safe_html_append(&out, z+m, k-m);
382f373… drh 3150 blob_append(&out, z+k, n-k);
382f373… drh 3151 }else{
382f373… drh 3152 safe_html_append(&out, z, n);
382f373… drh 3153 }
382f373… drh 3154 blob_reset(in);
382f373… drh 3155 *in = out;
382f373… drh 3156 }
aadf917… drh 3157
aadf917… drh 3158 /*
aadf917… drh 3159 ** COMMAND: test-safe-html
aadf917… drh 3160 **
fbffbe1… wyoung 3161 ** Usage: %fossil test-safe-html ?OPTIONS? FILE ...
aadf917… drh 3162 **
aadf917… drh 3163 ** Read files named on the command-line. Send the text of each file
54c1fd6… drh 3164 ** through safe_html_append() and then write the result on
aadf917… drh 3165 ** standard output.
fbffbe1… wyoung 3166 **
fbffbe1… wyoung 3167 ** Options:
fbffbe1… wyoung 3168 ** --top PATH Use PATH as g.zTop when rewriting root-relative URLs
aadf917… drh 3169 */
aadf917… drh 3170 void test_safe_html_cmd(void){
aadf917… drh 3171 int i;
aadf917… drh 3172 Blob x;
fbffbe1… wyoung 3173 const char *zTop = find_option("top",0,1);
fbffbe1… wyoung 3174 verify_all_options();
fbffbe1… wyoung 3175 if( zTop ) g.zTop = (char*)zTop;
aadf917… drh 3176 for(i=2; i<g.argc; i++){
aadf917… drh 3177 char *z;
aadf917… drh 3178 int n;
aadf917… drh 3179 blob_read_from_file(&x, g.argv[i], ExtFILE);
aadf917… drh 3180 blob_terminate(&x);
382f373… drh 3181 safe_html(&x);
382f373… drh 3182 z = blob_str(&x);
382f373… drh 3183 n = blob_size(&x);
aadf917… drh 3184 while( n>0 && (z[n-1]=='\n' || z[n-1]=='\r') ) n--;
aadf917… drh 3185 fossil_print("%.*s\n", n, z);
382f373… drh 3186 blob_reset(&x);
65870e8… drh 3187 }
dbda8d6… drh 3188 }

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button