00001
00008
00009
00010 #include "NMEStyle.h"
00011
00012 NMEOutputFormat const NMEOutputFormatBasicText =
00013 {
00014 " ",
00015 0,
00016 10,
00017 '%',
00018 "", "",
00019 4,
00020 "%%{i>0}%{i}. %%", "\n",
00021 "", "\n",
00022 "\n",
00023 "", "",
00024 "", "\n",
00025 FALSE,
00026 "", "",
00027 "- ", "\n",
00028 "", "",
00029 "%{i}. ", "\n",
00030 "", "",
00031 "", " ",
00032 NULL,
00033 "", "\n",
00034 "", "",
00035 "", "\n",
00036 "", "",
00037 "", "\n",
00038 "", "\t",
00039 "", "\t",
00040 "\n",
00041 "", "",
00042 "", "",
00043 "", "",
00044 "", "",
00045 "", "",
00046 "", "",
00047 "", "", NULL, FALSE,
00048 "", "", NULL, FALSE, FALSE,
00049 NULL,
00050 NULL, NULL,
00051 NULL, NULL,
00052 NULL, NULL,
00053 -1, NULL, NULL,
00054 NULL, NULL, NULL, NULL,
00055 NULL,
00056 NULL
00057 };
00058
00059 void NMEStyleInit(NMEStyleTable *table, NMEInt size,
00060 NMEBoolean convertOffsetsToUnicode)
00061 {
00062 table->tableSize = (size - (sizeof(NMEStyleTable) - sizeof(NMEStyleSpan)))
00063 / sizeof(NMEStyleSpan);
00064 table->n = 0;
00065 table->convertOffsetsToUnicode = convertOffsetsToUnicode;
00066 table->depth = 0;
00067 }
00068
00074 static NMEStyleEnum decodeStyle(NMEConstText markup, NMEInt level)
00075 {
00076 if (level == kNMEHookLevelSpan)
00077 switch (markup[0])
00078 {
00079 case '*':
00080 return kNMEStyleCharBold;
00081 case '/':
00082 return kNMEStyleCharItalic;
00083 case '_':
00084 return kNMEStyleCharUnderline;
00085 case '^':
00086 return kNMEStyleCharSuperscript;
00087 case ',':
00088 return kNMEStyleCharSubscript;
00089 case '#':
00090 return kNMEStyleCharMonospace;
00091 case '[':
00092 return kNMEStyleCharLink;
00093 case '{':
00094 return kNMEStyleCharImage;
00095 default:
00096 return kNMEStyleCharPlain;
00097 }
00098 else
00099 switch (markup[0])
00100 {
00101 case '{':
00102 return kNMEStyleCharMonospace;
00103 case '=':
00104 return kNMEStyleParHeading;
00105 case '*':
00106 return kNMEStyleParUL;
00107 case '#':
00108 return kNMEStyleParOL;
00109 case ';':
00110 return markup[1] == ':' ? kNMEStyleParDT : kNMEStyleParDL;
00111 case ':':
00112 return kNMEStyleParIndentedPar;
00113 case '|':
00114 return kNMEStyleParTable;
00115 default:
00116 return kNMEStyleParPlain;
00117 }
00118 }
00119
00120 NMEErr NMEStyleSpanHook(NMEInt level,
00121 NMEInt item,
00122 NMEBoolean enter,
00123 NMEConstText markup,
00124 NMEInt srcIndex,
00125 NMEInt srcLineNumber,
00126 NMEContext *context,
00127 void *data)
00128 {
00129 NMEStyleTable *table = (NMEStyleTable *)data;
00130 NMEStyleEnum style = decodeStyle(markup, level);
00131 NMEStyleEnum substyle = kNMEStyleCharPlain;
00132
00133 if (style == kNMEStyleCharPlain)
00134 return kNMEErrOk;
00135 else if (style == kNMEStyleParDL && markup[1] == '\0')
00136 substyle = kNMEStyleCharDT;
00137 else if (style == kNMEStyleParTable && markup[1] == '=')
00138 substyle = kNMEStyleCharTH;
00139
00140 if (enter)
00141 {
00142 if (table->n >= table->tableSize)
00143 return kNMEErrStyleTableTooSmall;
00144 table->span[table->n].begin
00145 = table->convertOffsetsToUnicode
00146 ? NMECurrentOutputIndexUCS16(context)
00147 : NMECurrentOutputIndex(context);
00148 table->span[table->n].style = style;
00149 table->span[table->n].level = level;
00150 NMECurrentLink(context,
00151 &table->span[table->n].linkOffset, &table->span[table->n].linkLength);
00152 table->spanStack[table->depth++] = table->n;
00153 table->n++;
00154 if (substyle != kNMEStyleCharPlain)
00155 {
00156 if (table->n >= table->tableSize)
00157 return kNMEErrStyleTableTooSmall;
00158 table->span[table->n] = table->span[table->n - 1];
00159 table->span[table->n].style = substyle;
00160 table->spanStack[table->depth++] = table->n;
00161 table->n++;
00162 }
00163 }
00164 else
00165 {
00166 NMEInt end = table->convertOffsetsToUnicode
00167 ? NMECurrentOutputIndexUCS16(context)
00168 : NMECurrentOutputIndex(context);
00169
00170 if (table->depth + (substyle != kNMEStyleCharPlain ? 1 : 0) <= 0)
00171 return kNMEErrInternal;
00172 if (substyle != kNMEStyleCharPlain)
00173 table->span[table->spanStack[--table->depth]].end = end;
00174 table->span[table->spanStack[--table->depth]].end = end;
00175 }
00176
00177 return kNMEErrOk;
00178 }