ext/generate.c in rdiscount-2.1.8 vs ext/generate.c in rdiscount-2.2.0
- old
+ new
@@ -784,11 +784,11 @@
static void
mangle(char *s, int len, MMIOT *f)
{
while ( len-- > 0 ) {
#if DEBIAN_GLITCH
- Qprintf(f, "d;", *((unsigned char*)(s++)) );
+ Qprintf(f, "&#%02d;", *((unsigned char*)(s++)) );
#else
Qstring("&#", f);
Qprintf(f, COINTOSS() ? "x%02x;" : "%02d;", *((unsigned char*)(s++)) );
#endif
}
@@ -858,11 +858,10 @@
cputc(s[++i], f);
else
cputc(c, f);
} /* code */
-
/* delspan() -- write out a chunk of text, blocking with <del>...</del>
*/
static void
delspan(MMIOT *f, int size)
{
@@ -1007,15 +1006,13 @@
if ( peek(f, size+2) != EOF )
size++;
}
else if ( isspace(c) )
break;
-#if WITH_GITHUB_TAGS
- else if ( ! (c == '/' || c == '-' || c == '_' || isalnum(c) ) )
-#else
- else if ( ! (c == '/' || isalnum(c) ) )
-#endif
+ else if ( ! (c == '/'
+ || (f->flags & MKD_GITHUBTAGS && (c == '-' || c == '_'))
+ || isalnum(c) ) )
maybetag=0;
}
if ( size ) {
if ( maybetag || (size >= 3 && strncmp(cursor(f), "!--", 3) == 0) ) {
@@ -1062,10 +1059,12 @@
for ( size=0; (c=peek(f, size+1)) != EOF; size++ ) {
if ( c == '\\' ) {
if ( peek(f, size+2) != EOF )
++size;
}
+ else if ( c & 0x80 ) /* HACK: ignore utf-8 extended characters */
+ continue;
else if ( isspace(c) || strchr("'\"()[]{}<>`", c) || c == MKD_EOLN )
break;
}
if ( (size > 1) && process_possible_link(f, size) ) {
@@ -1206,10 +1205,33 @@
}
return 0;
} /* smartypants */
+#if WITH_LATEX
+/* process latex with arbitrary 2-character ( $$ .. $$, \[ .. \], \( .. \)
+ * delimiters
+ */
+static int
+mathhandler(MMIOT *f, int e1, int e2)
+{
+ int i = 0;
+
+ while(peek(f, ++i) != EOF) {
+ if (peek(f, i) == e1 && peek(f, i+1) == e2) {
+ cputc(peek(f,-1), f);
+ cputc(peek(f, 0), f);
+ while ( i-- > -1 )
+ cputc(pull(f), f);
+ return 1;
+ }
+ }
+ return 0;
+}
+#endif
+
+
/* process a body of text encased in some sort of tick marks. If it
* works, generate the output and return 1, otherwise just return 0 and
* let the caller figure it out.
*/
static int
@@ -1281,10 +1303,11 @@
Qstring("![", f);
}
else
Qchar(c, f);
break;
+
case '[': if ( tag_text(f) || !linkylinky(0, f) )
Qchar(c, f);
break;
/* A^B -> A<sup>B</sup> */
case '^': if ( (f->flags & (MKD_NOSUPERSCRIPT|MKD_STRICT|MKD_TAGTEXT))
@@ -1384,11 +1407,18 @@
Qchar(c, f);
break;
case EOF: Qchar('\\', f);
break;
-
+
+#if WITH_LATEX
+ case '[':
+ case '(': if ( mathhandler(f, '\\', (c =='(')?')':']') )
+ break;
+ /* else fall through to default */
+#endif
+
default: if ( escaped(f,c) ||
strchr(">#.-+{}]![*_\\()`", c) )
Qchar(c, f);
else {
Qchar('\\', f);
@@ -1410,10 +1440,20 @@
Qstring("&", f);
else
Qchar(c, f);
break;
+#if WITH_LATEX
+ case '$': if ( peek(f, 1) == '$' ) {
+ pull(f);
+ if ( mathhandler(f, '$', '$') )
+ break;
+ Qchar('$', f);
+ }
+ /* fall through to default */
+#endif
+
default: Qchar(c, f);
break;
}
}
/* truncate the input string after we've finished processing it */
@@ -1424,30 +1464,30 @@
/* print a header block
*/
static void
printheader(Paragraph *pp, MMIOT *f)
{
-#if WITH_ID_ANCHOR
- Qprintf(f, "<h%d", pp->hnumber);
- if ( f->flags & MKD_TOC ) {
- Qstring(" id=\"", f);
- mkd_string_to_anchor(T(pp->text->text),
- S(pp->text->text),
- (mkd_sta_function_t)Qchar, f, 1);
- Qchar('"', f);
+ if ( f->flags & MKD_IDANCHOR ) {
+ Qprintf(f, "<h%d", pp->hnumber);
+ if ( f->flags & MKD_TOC ) {
+ Qstring(" id=\"", f);
+ mkd_string_to_anchor(T(pp->text->text),
+ S(pp->text->text),
+ (mkd_sta_function_t)Qchar, f, 1, f->flags);
+ Qchar('"', f);
+ }
+ Qchar('>', f);
+ } else {
+ if ( f->flags & MKD_TOC ) {
+ Qstring("<a name=\"", f);
+ mkd_string_to_anchor(T(pp->text->text),
+ S(pp->text->text),
+ (mkd_sta_function_t)Qchar, f, 1, f->flags);
+ Qstring("\"></a>\n", f);
+ }
+ Qprintf(f, "<h%d>", pp->hnumber);
}
- Qchar('>', f);
-#else
- if ( f->flags & MKD_TOC ) {
- Qstring("<a name=\"", f);
- mkd_string_to_anchor(T(pp->text->text),
- S(pp->text->text),
- (mkd_sta_function_t)Qchar, f, 1);
- Qstring("\"></a>\n", f);
- }
- Qprintf(f, "<h%d>", pp->hnumber);
-#endif
push(T(pp->text->text), S(pp->text->text), f);
text(f);
Qprintf(f, "</h%d>", pp->hnumber);
}
@@ -1810,18 +1850,22 @@
if ( ! p->html ) {
htmlify(p->code, 0, 0, p->ctx);
if ( p->ctx->flags & MKD_EXTRA_FOOTNOTE )
mkd_extra_footnotes(p->ctx);
p->html = 1;
+ size = S(p->ctx->out);
+
+ if ( (size == 0) || T(p->ctx->out)[size-1] ) {
+ /* Add a null byte at the end of the generated html,
+ * but pretend it doesn't exist.
+ */
+ EXPAND(p->ctx->out) = 0;
+ --S(p->ctx->out);
+ }
}
-
- size = S(p->ctx->out);
- if ( (size == 0) || T(p->ctx->out)[size-1] )
- EXPAND(p->ctx->out) = 0;
-
*res = T(p->ctx->out);
- return size;
+ return S(p->ctx->out);
}
return EOF;
}