| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
#include <stdio.h> |
| 8 |
#include <string.h> |
| 9 |
#include <stdarg.h> |
| 10 |
#include <stdlib.h> |
| 11 |
#include <time.h> |
| 12 |
#include <ctype.h> |
| 13 |
|
| 14 |
#include "config.h" |
| 15 |
|
| 16 |
#include "cstring.h" |
| 17 |
#include "markdown.h" |
| 18 |
#include "amalloc.h" |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
static int |
| 41 |
empair(MMIOT *f, int first, int last, int match) |
| 42 |
{ |
| 43 |
|
| 44 |
int i; |
| 45 |
block *begin, *p; |
| 46 |
|
| 47 |
begin = &T(f->Q)[first]; |
| 48 |
|
| 49 |
for (i=first+1; i <= last; i++) { |
| 50 |
p = &T(f->Q)[i]; |
| 51 |
|
| 52 |
if ( (p->b_type != bTEXT) && (p->b_count <= 0) ) |
| 53 |
continue; |
| 54 |
|
| 55 |
if ( p->b_type == begin->b_type ) { |
| 56 |
if ( p->b_count == match ) |
| 57 |
return i; |
| 58 |
|
| 59 |
if ( p->b_count > 2 ) |
| 60 |
return i; |
| 61 |
} |
| 62 |
} |
| 63 |
return 0; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
static void |
| 71 |
emfill(block *p) |
| 72 |
{ |
| 73 |
int j; |
| 74 |
|
| 75 |
if ( p->b_type == bTEXT ) |
| 76 |
return; |
| 77 |
|
| 78 |
for (j=0; j < p->b_count; j++) |
| 79 |
EXPAND(p->b_text) = p->b_char; |
| 80 |
p->b_count = 0; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
static void |
| 85 |
emclose(MMIOT *f, int first, int last) |
| 86 |
{ |
| 87 |
int j; |
| 88 |
|
| 89 |
for (j=first+1; j<last-1; j++) |
| 90 |
emfill(&T(f->Q)[j]); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
static struct emtags { |
| 95 |
char open[10]; |
| 96 |
char close[10]; |
| 97 |
int size; |
| 98 |
} emtags[] = { { "<em>" , "</em>", 5 }, { "<strong>", "</strong>", 9 } }; |
| 99 |
|
| 100 |
|
| 101 |
static void emblock(MMIOT*,int,int); |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
static void |
| 107 |
emmatch(MMIOT *f, int first, int last) |
| 108 |
{ |
| 109 |
block *start = &T(f->Q)[first]; |
| 110 |
int e, e2, match; |
| 111 |
|
| 112 |
switch (start->b_count) { |
| 113 |
case 2: if ( e = empair(f,first,last,match=2) ) |
| 114 |
break; |
| 115 |
case 1: e = empair(f,first,last,match=1); |
| 116 |
break; |
| 117 |
case 0: return; |
| 118 |
default: |
| 119 |
e = empair(f,first,last,1); |
| 120 |
e2= empair(f,first,last,2); |
| 121 |
|
| 122 |
if ( e2 >= e ) { |
| 123 |
e = e2; |
| 124 |
match = 2; |
| 125 |
} |
| 126 |
else |
| 127 |
match = 1; |
| 128 |
break; |
| 129 |
} |
| 130 |
|
| 131 |
if ( e ) { |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
block *end = &T(f->Q)[e]; |
| 138 |
|
| 139 |
end->b_count -= match; |
| 140 |
start->b_count -= match; |
| 141 |
|
| 142 |
emblock(f, first, e); |
| 143 |
|
| 144 |
PREFIX(start->b_text, emtags[match-1].open, emtags[match-1].size-1); |
| 145 |
SUFFIX(end->b_post, emtags[match-1].close, emtags[match-1].size); |
| 146 |
|
| 147 |
emmatch(f, first, last); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
static void |
| 155 |
emblock(MMIOT *f, int first, int last) |
| 156 |
{ |
| 157 |
int i; |
| 158 |
|
| 159 |
for ( i = first; i <= last; i++ ) |
| 160 |
if ( T(f->Q)[i].b_type != bTEXT ) |
| 161 |
emmatch(f, i, last); |
| 162 |
emclose(f, first, last); |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
void |
| 170 |
___mkd_emblock(MMIOT *f) |
| 171 |
{ |
| 172 |
int i; |
| 173 |
block *p; |
| 174 |
|
| 175 |
emblock(f, 0, S(f->Q)-1); |
| 176 |
|
| 177 |
for (i=0; i < S(f->Q); i++) { |
| 178 |
p = &T(f->Q)[i]; |
| 179 |
emfill(p); |
| 180 |
|
| 181 |
if ( S(p->b_post) ) { SUFFIX(f->out, T(p->b_post), S(p->b_post)); |
| 182 |
DELETE(p->b_post); } |
| 183 |
if ( S(p->b_text) ) { SUFFIX(f->out, T(p->b_text), S(p->b_text)); |
| 184 |
DELETE(p->b_text); } |
| 185 |
} |
| 186 |
|
| 187 |
S(f->Q) = 0; |
| 188 |
} |