/* ** Copyright (c) 2007 D. Richard Hipp ** ** This program is free software; you can redistribute it and/or ** modify it under the terms of the Simplified BSD License (also ** known as the "2-Clause License" or "FreeBSD License".) ** This program is distributed in the hope that it will be useful, ** but without any warranty; without even the implied warranty of ** merchantability or fitness for a particular purpose. ** ** Author contact information: ** drh@hwaci.com ** http://www.hwaci.com/drh/ ** ******************************************************************************* ** ** This file contains code to do formatting of wiki text. */ #include "config.h" #include #include "wikiformat.h" #if INTERFACE /* ** Allowed wiki transformation operations */ #define WIKI_HTMLONLY 0x001 /* HTML markup only. No wiki */ #define WIKI_INLINE 0x002 /* Do not surround with

..

*/ #define WIKI_NOBLOCK 0x004 /* No block markup of any kind */ #define WIKI_BUTTONS 0x008 /* Allow sub-menu buttons */ #define WIKI_NOBADLINKS 0x010 /* Ignore broken hyperlinks */ #define WIKI_LINKSONLY 0x020 /* No markup. Only decorate links */ #define WIKI_NEWLINE 0x040 /* Honor \n - break lines at each \n */ #define WIKI_MARKDOWNLINKS 0x080 /* Resolve hyperlinks as in markdown */ #define WIKI_SAFE 0x100 /* Make the result safe for embedding */ #define WIKI_TARGET_BLANK 0x200 /* Hyperlinks go to a new window */ #define WIKI_NOBRACKET 0x400 /* Omit extra [..] around hyperlinks */ #endif /* ** These are the only markup attributes allowed. */ enum allowed_attr_t { ATTR_ALIGN = 1, ATTR_ALT, ATTR_BGCOLOR, ATTR_BORDER, ATTR_CELLPADDING, ATTR_CELLSPACING, ATTR_CLASS, ATTR_CLEAR, ATTR_COLOR, ATTR_COLSPAN, ATTR_COMPACT, ATTR_FACE, ATTR_HEIGHT, ATTR_HREF, ATTR_HSPACE, ATTR_ID, ATTR_LINKS, ATTR_NAME, ATTR_ROWSPAN, ATTR_SIZE, ATTR_SRC, ATTR_START, ATTR_STYLE, ATTR_TARGET, ATTR_TITLE, ATTR_TYPE, ATTR_VALIGN, ATTR_VALUE, ATTR_VSPACE, ATTR_WIDTH }; enum amsk_t { AMSK_ALIGN = 0x00000001, AMSK_ALT = 0x00000002, AMSK_BGCOLOR = 0x00000004, AMSK_BORDER = 0x00000008, AMSK_CELLPADDING = 0x00000010, AMSK_CELLSPACING = 0x00000020, AMSK_CLASS = 0x00000040, AMSK_CLEAR = 0x00000080, AMSK_COLOR = 0x00000100, AMSK_COLSPAN = 0x00000200, AMSK_COMPACT = 0x00000400, AMSK_FACE = 0x00000800, AMSK_HEIGHT = 0x00001000, AMSK_HREF = 0x00002000, AMSK_HSPACE = 0x00004000, AMSK_ID = 0x00008000, AMSK_LINKS = 0x00010000, AMSK_NAME = 0x00020000, AMSK_ROWSPAN = 0x00040000, AMSK_SIZE = 0x00080000, AMSK_SRC = 0x00100000, AMSK_START = 0x00200000, AMSK_STYLE = 0x00400000, AMSK_TARGET = 0x00800000, AMSK_TITLE = 0x01000000, AMSK_TYPE = 0x02000000, AMSK_VALIGN = 0x04000000, AMSK_VALUE = 0x08000000, AMSK_VSPACE = 0x10000000, AMSK_WIDTH = 0x20000000 }; static const struct AllowedAttribute { const char *zName; unsigned int iMask; } aAttribute[] = { /* These indexes MUST line up with their corresponding allowed_attr_t enum values. */ { 0, 0 }, { "align", AMSK_ALIGN }, { "alt", AMSK_ALT }, { "bgcolor", AMSK_BGCOLOR }, { "border", AMSK_BORDER }, { "cellpadding", AMSK_CELLPADDING }, { "cellspacing", AMSK_CELLSPACING }, { "class", AMSK_CLASS }, { "clear", AMSK_CLEAR }, { "color", AMSK_COLOR }, { "colspan", AMSK_COLSPAN }, { "compact", AMSK_COMPACT }, { "face", AMSK_FACE }, { "height", AMSK_HEIGHT }, { "href", AMSK_HREF }, { "hspace", AMSK_HSPACE }, { "id", AMSK_ID }, { "links", AMSK_LINKS }, { "name", AMSK_NAME }, { "rowspan", AMSK_ROWSPAN }, { "size", AMSK_SIZE }, { "src", AMSK_SRC }, { "start", AMSK_START }, { "style", AMSK_STYLE }, { "target", AMSK_TARGET }, { "title", AMSK_TITLE }, { "type", AMSK_TYPE }, { "valign", AMSK_VALIGN }, { "value", AMSK_VALUE }, { "vspace", AMSK_VSPACE }, { "width", AMSK_WIDTH }, }; /* ** Use binary search to locate a tag in the aAttribute[] table. */ static int findAttr(const char *z){ int i, c, first, last; first = 1; last = count(aAttribute) - 1; while( first<=last ){ i = (first+last)/2; c = fossil_strcmp(aAttribute[i].zName, z); if( c==0 ){ return i; }else if( c<0 ){ first = i+1; }else{ last = i-1; } } return 0; } /* ** Allowed markup. ** ** Except for MARKUP_INVALID, this must all be in alphabetical order ** and in numerical sequence. The first markup type must be zero. ** The value for MARKUP_XYZ must correspond to the entry ** in aMarkup[]. */ enum markup_t { MARKUP_INVALID = 0, MARKUP_A, MARKUP_ABBR, MARKUP_ADDRESS, MARKUP_HTML5_ARTICLE, MARKUP_HTML5_ASIDE, MARKUP_B, MARKUP_BIG, MARKUP_BLOCKQUOTE, MARKUP_BR, MARKUP_CENTER, MARKUP_CITE, MARKUP_CODE, MARKUP_COL, MARKUP_COLGROUP, MARKUP_DD, MARKUP_DEL, MARKUP_DETAILS, MARKUP_DFN, MARKUP_DIV, MARKUP_DL, MARKUP_DT, MARKUP_EM, MARKUP_FONT, MARKUP_HTML5_FOOTER, MARKUP_H1, MARKUP_H2, MARKUP_H3, MARKUP_H4, MARKUP_H5, MARKUP_H6, MARKUP_HTML5_HEADER, MARKUP_HR, MARKUP_I, MARKUP_IMG, MARKUP_INS, MARKUP_KBD, MARKUP_LI, MARKUP_HTML5_NAV, MARKUP_NOBR, MARKUP_NOWIKI, MARKUP_OL, MARKUP_P, MARKUP_PRE, MARKUP_S, MARKUP_SAMP, MARKUP_HTML5_SECTION, MARKUP_SMALL, MARKUP_SPAN, MARKUP_STRIKE, MARKUP_STRONG, MARKUP_SUB, MARKUP_SUMMARY, MARKUP_SUP, MARKUP_TABLE, MARKUP_TBODY, MARKUP_TD, MARKUP_TFOOT, MARKUP_TH, MARKUP_THEAD, MARKUP_TITLE, MARKUP_TR, MARKUP_TT, MARKUP_U, MARKUP_UL, MARKUP_VAR, MARKUP_VERBATIM }; /* ** The various markup is divided into the following types: */ #define MUTYPE_SINGLE 0x0001 /* ,
, or
*/ #define MUTYPE_BLOCK 0x0002 /* Forms a new paragraph. ex:

,

*/ #define MUTYPE_FONT 0x0004 /* Font changes. ex: , , */ #define MUTYPE_LIST 0x0010 /* Lists.
    ,