Fossil SCM

Do not duplicate markdown titles in the title of the page and in the body of the document.

drh 2015-12-03 15:19 trunk
Commit d407c3842116e163fce36055c3998e08731fc6f9
2 files changed +15 -7 +1 -8
--- src/markdown_html.c
+++ src/markdown_html.c
@@ -100,21 +100,23 @@
100100
}
101101
102102
static void html_raw_block(struct Blob *ob, struct Blob *text, void *opaque){
103103
char *data = blob_buffer(text);
104104
size_t size = blob_size(text);
105
+ Blob *title = (Blob*)opaque;
105106
while( size>0 && fossil_isspace(data[0]) ){ data++; size--; }
106107
while( size>0 && fossil_isspace(data[size-1]) ){ size--; }
107108
/* If the first raw block is an <h1> element, then use it as the title. */
108109
if( blob_size(ob)<=PROLOG_SIZE
109110
&& size>9
111
+ && title!=0
110112
&& sqlite3_strnicmp("<h1",data,3)==0
111113
&& sqlite3_strnicmp("</h1>", &data[size-5],5)==0
112114
){
113
- Blob *title = (Blob*)opaque;
114115
int nTag = htmlTagLength(data);
115116
blob_append(title, data+nTag, size - nTag - 5);
117
+ return;
116118
}
117119
INTER_BLOCK(ob);
118120
blob_append(ob, data, size);
119121
BLOB_APPEND_LITERAL(ob, "\n");
120122
}
@@ -140,12 +142,13 @@
140142
void *opaque
141143
){
142144
struct Blob *title = opaque;
143145
/* The first header at the beginning of a text is considered as
144146
* a title and not output. */
145
- if( blob_size(ob)<=PROLOG_SIZE && blob_size(title)==0 ){
147
+ if( blob_size(ob)<=PROLOG_SIZE && title!=0 && blob_size(title)==0 ){
146148
BLOB_APPEND_BLOB(title, text);
149
+ return;
147150
}
148151
INTER_BLOCK(ob);
149152
blob_appendf(ob, "<h%d>", level);
150153
BLOB_APPEND_BLOB(ob, text);
151154
blob_appendf(ob, "</h%d>", level);
@@ -381,15 +384,20 @@
381384
382385
static void html_normal_text(struct Blob *ob, struct Blob *text, void *opaque){
383386
html_escape(ob, blob_buffer(text), blob_size(text));
384387
}
385388
386
-
389
+/*
390
+** Convert markdown into HTML.
391
+**
392
+** The document title is placed in output_title if not NULL. Or if
393
+** output_title is NULL, the document title appears in the body.
394
+*/
387395
void markdown_to_html(
388
- struct Blob *input_markdown,
389
- struct Blob *output_title,
390
- struct Blob *output_body
396
+ struct Blob *input_markdown, /* Markdown content to be rendered */
397
+ struct Blob *output_title, /* Put title here. May be NULL */
398
+ struct Blob *output_body /* Put document body here. */
391399
){
392400
struct mkd_renderer html_renderer = {
393401
/* prolog and epilog */
394402
html_prolog,
395403
html_epilog,
@@ -426,9 +434,9 @@
426434
64, /* maximum stack */
427435
"*_", /* emphasis characters */
428436
0 /* opaque data */
429437
};
430438
html_renderer.opaque = output_title;
431
- blob_reset(output_title);
439
+ if( output_title ) blob_reset(output_title);
432440
blob_reset(output_body);
433441
markdown(output_body, input_markdown, &html_renderer);
434442
}
435443
--- src/markdown_html.c
+++ src/markdown_html.c
@@ -100,21 +100,23 @@
100 }
101
102 static void html_raw_block(struct Blob *ob, struct Blob *text, void *opaque){
103 char *data = blob_buffer(text);
104 size_t size = blob_size(text);
 
105 while( size>0 && fossil_isspace(data[0]) ){ data++; size--; }
106 while( size>0 && fossil_isspace(data[size-1]) ){ size--; }
107 /* If the first raw block is an <h1> element, then use it as the title. */
108 if( blob_size(ob)<=PROLOG_SIZE
109 && size>9
 
110 && sqlite3_strnicmp("<h1",data,3)==0
111 && sqlite3_strnicmp("</h1>", &data[size-5],5)==0
112 ){
113 Blob *title = (Blob*)opaque;
114 int nTag = htmlTagLength(data);
115 blob_append(title, data+nTag, size - nTag - 5);
 
116 }
117 INTER_BLOCK(ob);
118 blob_append(ob, data, size);
119 BLOB_APPEND_LITERAL(ob, "\n");
120 }
@@ -140,12 +142,13 @@
140 void *opaque
141 ){
142 struct Blob *title = opaque;
143 /* The first header at the beginning of a text is considered as
144 * a title and not output. */
145 if( blob_size(ob)<=PROLOG_SIZE && blob_size(title)==0 ){
146 BLOB_APPEND_BLOB(title, text);
 
147 }
148 INTER_BLOCK(ob);
149 blob_appendf(ob, "<h%d>", level);
150 BLOB_APPEND_BLOB(ob, text);
151 blob_appendf(ob, "</h%d>", level);
@@ -381,15 +384,20 @@
381
382 static void html_normal_text(struct Blob *ob, struct Blob *text, void *opaque){
383 html_escape(ob, blob_buffer(text), blob_size(text));
384 }
385
386
 
 
 
 
 
387 void markdown_to_html(
388 struct Blob *input_markdown,
389 struct Blob *output_title,
390 struct Blob *output_body
391 ){
392 struct mkd_renderer html_renderer = {
393 /* prolog and epilog */
394 html_prolog,
395 html_epilog,
@@ -426,9 +434,9 @@
426 64, /* maximum stack */
427 "*_", /* emphasis characters */
428 0 /* opaque data */
429 };
430 html_renderer.opaque = output_title;
431 blob_reset(output_title);
432 blob_reset(output_body);
433 markdown(output_body, input_markdown, &html_renderer);
434 }
435
--- src/markdown_html.c
+++ src/markdown_html.c
@@ -100,21 +100,23 @@
100 }
101
102 static void html_raw_block(struct Blob *ob, struct Blob *text, void *opaque){
103 char *data = blob_buffer(text);
104 size_t size = blob_size(text);
105 Blob *title = (Blob*)opaque;
106 while( size>0 && fossil_isspace(data[0]) ){ data++; size--; }
107 while( size>0 && fossil_isspace(data[size-1]) ){ size--; }
108 /* If the first raw block is an <h1> element, then use it as the title. */
109 if( blob_size(ob)<=PROLOG_SIZE
110 && size>9
111 && title!=0
112 && sqlite3_strnicmp("<h1",data,3)==0
113 && sqlite3_strnicmp("</h1>", &data[size-5],5)==0
114 ){
 
115 int nTag = htmlTagLength(data);
116 blob_append(title, data+nTag, size - nTag - 5);
117 return;
118 }
119 INTER_BLOCK(ob);
120 blob_append(ob, data, size);
121 BLOB_APPEND_LITERAL(ob, "\n");
122 }
@@ -140,12 +142,13 @@
142 void *opaque
143 ){
144 struct Blob *title = opaque;
145 /* The first header at the beginning of a text is considered as
146 * a title and not output. */
147 if( blob_size(ob)<=PROLOG_SIZE && title!=0 && blob_size(title)==0 ){
148 BLOB_APPEND_BLOB(title, text);
149 return;
150 }
151 INTER_BLOCK(ob);
152 blob_appendf(ob, "<h%d>", level);
153 BLOB_APPEND_BLOB(ob, text);
154 blob_appendf(ob, "</h%d>", level);
@@ -381,15 +384,20 @@
384
385 static void html_normal_text(struct Blob *ob, struct Blob *text, void *opaque){
386 html_escape(ob, blob_buffer(text), blob_size(text));
387 }
388
389 /*
390 ** Convert markdown into HTML.
391 **
392 ** The document title is placed in output_title if not NULL. Or if
393 ** output_title is NULL, the document title appears in the body.
394 */
395 void markdown_to_html(
396 struct Blob *input_markdown, /* Markdown content to be rendered */
397 struct Blob *output_title, /* Put title here. May be NULL */
398 struct Blob *output_body /* Put document body here. */
399 ){
400 struct mkd_renderer html_renderer = {
401 /* prolog and epilog */
402 html_prolog,
403 html_epilog,
@@ -426,9 +434,9 @@
434 64, /* maximum stack */
435 "*_", /* emphasis characters */
436 0 /* opaque data */
437 };
438 html_renderer.opaque = output_title;
439 if( output_title ) blob_reset(output_title);
440 blob_reset(output_body);
441 markdown(output_body, input_markdown, &html_renderer);
442 }
443
+1 -8
--- src/wiki.c
+++ src/wiki.c
@@ -147,20 +147,13 @@
147147
*/
148148
void wiki_render_by_mimetype(Blob *pWiki, const char *zMimetype){
149149
if( zMimetype==0 || fossil_strcmp(zMimetype, "text/x-fossil-wiki")==0 ){
150150
wiki_convert(pWiki, 0, 0);
151151
}else if( fossil_strcmp(zMimetype, "text/x-markdown")==0 ){
152
- Blob title = BLOB_INITIALIZER;
153152
Blob tail = BLOB_INITIALIZER;
154
- markdown_to_html(pWiki, &title, &tail);
155
-#if 0
156
- if( blob_size(&title)>0 ){
157
- @ <h1>%s(blob_str(&title))</h1>
158
- }
159
-#endif
153
+ markdown_to_html(pWiki, 0, &tail);
160154
@ %s(blob_str(&tail))
161
- blob_reset(&title);
162155
blob_reset(&tail);
163156
}else{
164157
@ <pre>
165158
@ %h(blob_str(pWiki))
166159
@ </pre>
167160
--- src/wiki.c
+++ src/wiki.c
@@ -147,20 +147,13 @@
147 */
148 void wiki_render_by_mimetype(Blob *pWiki, const char *zMimetype){
149 if( zMimetype==0 || fossil_strcmp(zMimetype, "text/x-fossil-wiki")==0 ){
150 wiki_convert(pWiki, 0, 0);
151 }else if( fossil_strcmp(zMimetype, "text/x-markdown")==0 ){
152 Blob title = BLOB_INITIALIZER;
153 Blob tail = BLOB_INITIALIZER;
154 markdown_to_html(pWiki, &title, &tail);
155 #if 0
156 if( blob_size(&title)>0 ){
157 @ <h1>%s(blob_str(&title))</h1>
158 }
159 #endif
160 @ %s(blob_str(&tail))
161 blob_reset(&title);
162 blob_reset(&tail);
163 }else{
164 @ <pre>
165 @ %h(blob_str(pWiki))
166 @ </pre>
167
--- src/wiki.c
+++ src/wiki.c
@@ -147,20 +147,13 @@
147 */
148 void wiki_render_by_mimetype(Blob *pWiki, const char *zMimetype){
149 if( zMimetype==0 || fossil_strcmp(zMimetype, "text/x-fossil-wiki")==0 ){
150 wiki_convert(pWiki, 0, 0);
151 }else if( fossil_strcmp(zMimetype, "text/x-markdown")==0 ){
 
152 Blob tail = BLOB_INITIALIZER;
153 markdown_to_html(pWiki, 0, &tail);
 
 
 
 
 
154 @ %s(blob_str(&tail))
 
155 blob_reset(&tail);
156 }else{
157 @ <pre>
158 @ %h(blob_str(pWiki))
159 @ </pre>
160

Keyboard Shortcuts

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