чување a2b0a8597fe6c742732aafba7a19e7c03126ae43
родитељ 22a149d7835297f193d0442c0d9890e61e25f9e2
Аутор: Страхиња Радић <contact@strahinja.org>
Датум: Fri, 26 Mar 2021 10:35:22 +0100
Added --global-link-prefix; more spacing fixes
Signed-off-by: Страхиња Радић <contact@strahinja.org>
Diffstat:
| M | defs.h | | | 1 | + |
| M | slw2gmi.1.in | | | 11 | +++++++++++ |
| M | slw2gmi.c | | | 438 | ++++++++++++++++++++++++++++++++++++++++++------------------------------------- |
измењених датотека: 3, додавања: 245(+), брисања: 205(-)
diff --git a/defs.h b/defs.h
@@ -71,6 +71,7 @@ typedef enum
CMD_BODY_ONLY,
CMD_BASEDIR,
CMD_HELP,
+ CMD_LINK_PREFIX,
CMD_VERSION
} Command;
diff --git a/slw2gmi.1.in b/slw2gmi.1.in
@@ -29,6 +29,7 @@ slw2gmi \- slweb to Gemini converter
.
.SY slw2gmi
.OP "\-d \fR|\fP \-\-basedir" directory
+.OP "\-p \fR|\fP \-\-global-link-prefix" directory
.RI [ filename ]
.YS
.
@@ -68,6 +69,16 @@ command). Defaults to the current directory.
Print this usage information screen.
.
.TP
+.BI \-p " directory"
+.TQ
+.BI \-\-global-link-prefix " directory"
+.br
+Set
+.I directory
+as a global prefix for relative links. For example, this can be useful when
+generating Gemini code which needs to keep relative links pointing to web URLs.
+.
+.TP
.B \-v
.TQ
.B \-\-version
diff --git a/slw2gmi.c b/slw2gmi.c
@@ -22,11 +22,14 @@
static size_t lineno = 0;
static size_t colno = 1;
static BOOL output_firstcol = TRUE;
+static BOOL skip_para_end = FALSE;
static char* input_filename = NULL;
static char* input_dirname = NULL;
static char* basedir = NULL;
static size_t basedir_size = 0;
static char* incdir = NULL;
+static char* global_link_prefix = NULL;
+static size_t global_link_prefix_size = 0;
static KeyValue* vars = NULL;
static KeyValue* pvars = NULL;
static size_t vars_count = 0;
@@ -93,8 +96,9 @@ version()
int
usage()
{
- printf("Usage: %s [-b|--body-only] [-d|--basedir <dir>] [-h|--help]"
- " [-v|--version] [filename]\n", PROGRAMNAME);
+ printf("Usage: %s [-b|--body-only] [-d|--basedir <dir>]"
+ " [-p|--global-link-prefix <dir>] [-h|--help]"
+ " [-v|--version] [filename]\n", PROGRAMNAME);
return 0;
}
@@ -221,6 +225,26 @@ set_basedir(char* arg, char** basedir, size_t* basedir_size)
return 0;
}
+int
+set_global_link_prefix(char* arg, char** global_link_prefix,
+ size_t* global_link_prefix_size)
+{
+ size_t arg_len = strlen(arg);
+
+ if (arg_len < 1)
+ exit(error(1, (uint8_t*)"--global-link-prefix: Argument required"));
+
+ if (arg_len + 1 > *global_link_prefix_size)
+ {
+ *global_link_prefix_size = arg_len+1;
+ REALLOC(*global_link_prefix, char, *global_link_prefix_size)
+ }
+ strncpy(*global_link_prefix, arg, *global_link_prefix_size-1);
+ *(*global_link_prefix + arg_len) = 0;
+
+ return 0;
+}
+
char*
strip_ext(const char* fn)
{
@@ -845,12 +869,14 @@ process_include(uint8_t* token, FILE* output, BOOL read_yaml_macros_and_links)
snprintf(input_filename, BUFSIZE, "%s/%s.slw", basedir, include_filename);
free(include_filename);
- FILE* input = NULL;
- FILE* output = stdout;
- uint8_t* buffer = NULL;
- size_t buffer_size = 0;
- int result = 0;
- ULONG saved_state = ST_NONE;
+ FILE* input = NULL;
+ FILE* output = stdout;
+ uint8_t* buffer = NULL;
+ size_t buffer_size = 0;
+ int result = 0;
+ ULONG saved_state = ST_NONE;
+ BOOL saved_output_firstcol = TRUE;
+ BOOL saved_skip_para_end = FALSE;
read_file_into_buffer(&buffer, &buffer_size, input_filename,
&input_dirname, &input);
@@ -884,7 +910,11 @@ process_include(uint8_t* token, FILE* output, BOOL read_yaml_macros_and_links)
current_inline_footnote = 0;
saved_state = state;
+ saved_output_firstcol = output_firstcol;
+ saved_skip_para_end = skip_para_end;
state = ST_NONE;
+ output_firstcol = TRUE;
+ skip_para_end = FALSE;
/* First pass: read YAML, macros and links */
result = slweb_parse(buffer, output, TRUE);
@@ -896,6 +926,8 @@ process_include(uint8_t* token, FILE* output, BOOL read_yaml_macros_and_links)
return result;
}
state = ST_NONE;
+ output_firstcol = TRUE;
+ skip_para_end = FALSE;
current_footnote = 0;
current_inline_footnote = 0;
@@ -903,6 +935,8 @@ process_include(uint8_t* token, FILE* output, BOOL read_yaml_macros_and_links)
result = slweb_parse(buffer, output, FALSE);
state = saved_state;
+ output_firstcol = saved_output_firstcol;
+ skip_para_end = saved_skip_para_end;
free(input_filename);
free(buffer);
@@ -937,7 +971,7 @@ process_list_item_end(const uint8_t* link_prefix, FILE* output)
if (state & ST_PARA_OPEN)
{
state &= ~ST_PARA_OPEN;
- fprintf(output, "\n");
+ print_output(output, "\n\n");
output_firstcol = TRUE;
process_paralinks(link_prefix, output);
}
@@ -1061,12 +1095,14 @@ process_incdir_subdir(const char* subdirname, const uint8_t* link_prefix,
snprintf(input_filename, BUFSIZE, "%s/%s", abs_subdirname,
(*pnamelist)->d_name);
- FILE* input = NULL;
- FILE* output = stdout;
- uint8_t* buffer = NULL;
- size_t buffer_size = 0;
- int result = 0;
- ULONG saved_state = ST_NONE;
+ FILE* input = NULL;
+ FILE* output = stdout;
+ uint8_t* buffer = NULL;
+ size_t buffer_size = 0;
+ int result = 0;
+ ULONG saved_state = ST_NONE;
+ BOOL saved_output_firstcol = TRUE;
+ BOOL saved_skip_para_end = FALSE;
read_file_into_buffer(&buffer, &buffer_size, input_filename,
&input_dirname, &input);
@@ -1100,10 +1136,11 @@ process_incdir_subdir(const char* subdirname, const uint8_t* link_prefix,
current_inline_footnote = 0;
saved_state = state;
+ saved_output_firstcol = output_firstcol;
+ saved_skip_para_end = skip_para_end;
state = ST_NONE;
-
- saved_state = state;
- state = ST_NONE;
+ output_firstcol = TRUE;
+ skip_para_end = FALSE;
/* Read YAML, macros and links */
result = slweb_parse(buffer, output, TRUE);
@@ -1125,11 +1162,10 @@ process_incdir_subdir(const char* subdirname, const uint8_t* link_prefix,
NULL);
if (title && date)
process_timestamp(output, link_prefix, link, NULL, date, title);
- /*
- *print_output(output, "%s%s", link, title);
- */
state = saved_state;
+ output_firstcol = saved_output_firstcol;
+ skip_para_end = saved_skip_para_end;
fflush(output);
free(buffer);
@@ -1308,12 +1344,16 @@ process_timestamp(FILE* output, const uint8_t* link_prefix, const char* link,
ptimestamp_format++;
}
if (title)
- print_output(output, "=> %s%s.gmi\t%s [%s]\n",
+ print_output(output, "=> %s%s%s.gmi\t%s [%s]\n",
+ url_is_local(link) && global_link_prefix
+ ? (const char*)global_link_prefix : "",
url_is_local(link) && link_prefix
? (const char*)link_prefix : "",
link, title, formatted_date);
else
- print_output(output, "=> %s%s.gmi\t%s\n",
+ print_output(output, "=> %s%s%s.gmi\t%s\n",
+ url_is_local(link) && global_link_prefix
+ ? (const char*)global_link_prefix : "",
url_is_local(link) && link_prefix
? (const char*)link_prefix : "",
link, formatted_date);
@@ -1389,8 +1429,12 @@ process_macro(uint8_t* token, FILE* output, BOOL read_yaml_macros_and_links,
}
int
+process_para_end(const uint8_t* link_prefix, BOOL previous_line_blank, FILE* output);
+
+int
process_tag(uint8_t* token, const uint8_t* link_prefix, FILE* output,
- BOOL read_yaml_macros_and_links, BOOL* skip_eol, BOOL end_tag)
+ BOOL previous_line_blank, BOOL read_yaml_macros_and_links,
+ BOOL* skip_eol, BOOL end_tag)
{
if (!token || u8_strlen(token) < 1)
return warning(1, (uint8_t*)"%s:%ld:%ld: Empty tag name",
@@ -1427,6 +1471,20 @@ process_tag(uint8_t* token, const uint8_t* link_prefix, FILE* output,
process_macro(token, output, read_yaml_macros_and_links, end_tag);
*skip_eol = TRUE;
}
+ else if (startswith((char*)token, "p") && !read_yaml_macros_and_links)
+ {
+ skip_para_end = TRUE;
+ *skip_eol = TRUE;
+ if (end_tag)
+ {
+ /*print_output(output, "ET");*/
+ process_para_end(link_prefix, previous_line_blank, output);
+ }
+ /*
+ *else
+ * print_output(output, "!ET!");
+ */
+ }
return 0;
}
@@ -1628,14 +1686,19 @@ int
process_paralinks(const uint8_t* link_prefix, FILE* output)
{
KeyValue* pparalink = paralinks;
- if (!output_firstcol && (paralinks_count > 0))
+ if (!skip_para_end)
+ {
+ if (!output_firstcol && (paralinks_count > 0))
+ print_output(output, "\n");
print_output(output, "\n");
- print_output(output, "\n");
+ }
while (pparalink < paralinks + paralinks_count)
{
if (pparalink->key)
{
- print_output(output, "=> %s%s\t%s\n",
+ print_output(output, "=> %s%s%s\t%s\n",
+ url_is_local((const char*)pparalink->key) && global_link_prefix
+ ? (const char*)global_link_prefix : "",
url_is_local((const char*)pparalink->key) && link_prefix
? (const char*)link_prefix : "",
pparalink->key, pparalink->value);
@@ -1659,10 +1722,21 @@ process_paralinks(const uint8_t* link_prefix, FILE* output)
}
int
+process_para_end(const uint8_t* link_prefix, BOOL previous_line_blank, FILE* output)
+{
+ if (!skip_para_end)
+ print_output(output, "\n");
+ output_firstcol = TRUE;
+ process_paralinks(link_prefix, output);
+
+ return 0;
+}
+
+int
process_line_start(uint8_t* line, const uint8_t* link_prefix,
BOOL first_line_in_doc, BOOL previous_line_blank,
- BOOL read_yaml_macros_and_links, BOOL list_para, FILE* output,
- uint8_t** token, uint8_t** ptoken)
+ BOOL read_yaml_macros_and_links, BOOL skip_eol, BOOL list_para,
+ FILE* output, uint8_t** token, uint8_t** ptoken)
{
if ((first_line_in_doc || previous_line_blank)
&& !(ANY(state, ST_BLOCKQUOTE | ST_PRE)))
@@ -1692,11 +1766,7 @@ process_line_start(uint8_t* line, const uint8_t* link_prefix,
if (state & ST_FOOTNOTE_TEXT)
{
if (!read_yaml_macros_and_links && (state & ST_PARA_OPEN))
- {
- print_output(output, "\n\n");
- output_firstcol = TRUE;
- process_paralinks(link_prefix, output);
- }
+ process_para_end(link_prefix, previous_line_blank, output);
state &= ~(ST_FOOTNOTE_TEXT | ST_PARA_OPEN);
}
}
@@ -1706,6 +1776,7 @@ process_line_start(uint8_t* line, const uint8_t* link_prefix,
print_output(output, "%s",
output_firstcol && previous_line_blank ? "" : "\n");
output_firstcol = TRUE;
+ skip_para_end = FALSE;
state |= ST_PARA_OPEN;
}
}
@@ -1714,17 +1785,18 @@ process_line_start(uint8_t* line, const uint8_t* link_prefix,
int
process_text_token(uint8_t* line, const uint8_t* link_prefix,
- BOOL first_line_in_doc, BOOL previous_line_blank, BOOL
- processed_start_of_line, BOOL read_yaml_macros_and_links,
- BOOL list_para, FILE* output, uint8_t** token, uint8_t** ptoken,
- size_t* token_size, BOOL add_enclosing_paragraph)
+ BOOL first_line_in_doc, BOOL previous_line_blank,
+ BOOL processed_start_of_line, BOOL skip_eol,
+ BOOL read_yaml_macros_and_links, BOOL list_para, FILE* output,
+ uint8_t** token, uint8_t** ptoken, size_t* token_size,
+ BOOL add_enclosing_paragraph)
{
if (!(state & ST_YAML))
{
if (add_enclosing_paragraph && !processed_start_of_line)
process_line_start(line, link_prefix, first_line_in_doc,
- previous_line_blank, read_yaml_macros_and_links, list_para,
- output, token, ptoken);
+ previous_line_blank, read_yaml_macros_and_links, skip_eol,
+ list_para, output, token, ptoken);
**ptoken = 0;
if (**token && !read_yaml_macros_and_links
&& !(state & ST_MACRO_BODY))
@@ -1802,11 +1874,12 @@ process_footnote(uint8_t* token, BOOL footnote_definition, BOOL footnote_output,
}
int
-process_horizontal_rule(FILE* output)
+process_horizontal_rule(const uint8_t* link_prefix, BOOL previous_line_blank,
+ FILE* output)
{
/* Temporarily break paragraph as hr is para-level */
if (state & ST_PARA_OPEN)
- print_output(output, "\n\n");
+ process_para_end(link_prefix, previous_line_blank, output);
print_output(output, "————————————————————\n");
if (state & ST_PARA_OPEN)
print_output(output, "\n");
@@ -1861,19 +1934,20 @@ begin_article(FILE* output, const BOOL add_article_header,
}
int
-end_footnotes(FILE* output, const uint8_t* link_prefix, BOOL add_footnote_div)
+end_footnotes(FILE* output, const uint8_t* link_prefix, BOOL add_footnote_div,
+ BOOL previous_line_blank)
{
size_t footnote = 0;
if (state & ST_PARA_OPEN)
{
- print_output(output, "\n\n");
output_firstcol = TRUE;
state &= ~ST_PARA_OPEN;
- process_paralinks(link_prefix, output);
+ if (paralinks_count > 0)
+ process_paralinks(link_prefix, output);
}
- process_horizontal_rule(output);
+ process_horizontal_rule(link_prefix, previous_line_blank, output);
for (footnote = 0; footnote < inline_footnote_count; footnote++)
{
@@ -2033,7 +2107,8 @@ slweb_parse(uint8_t* buffer, FILE* output, BOOL read_yaml_macros_and_links)
if (!(state & ST_YAML) && lineno > 1
&& !read_yaml_macros_and_links)
- process_horizontal_rule(output);
+ process_horizontal_rule(link_prefix, previous_line_blank,
+ output);
else
{
if (lineno == 1)
@@ -2159,37 +2234,16 @@ slweb_parse(uint8_t* buffer, FILE* output, BOOL read_yaml_macros_and_links)
}
else if (!ANY(state, ST_PRE | ST_TAG | ST_YAML))
{
- /* Handle ` within footnotes, headings and link text specially */
- if (ANY(state, ST_INLINE_FOOTNOTE | ST_HEADING
+ if (!ANY(state, ST_INLINE_FOOTNOTE | ST_HEADING
| ST_FOOTNOTE_TEXT | ST_LINK))
{
- uint8_t* tag = state & ST_CODE
- ? (uint8_t*)"</code>"
- : (uint8_t*)"<code>";
- size_t tag_len = u8_strlen(tag);
- size_t token_len = 0;
-
- *ptoken = 0;
- token_len = u8_strlen(token);
-
- if (token_len + tag_len < BUFSIZE)
- {
- u8_strncat(token, tag, BUFSIZE-token_len-1);
- ptoken += tag_len;
- }
- }
- else
- {
/* Output existing text up to ` */
process_text_token(line, link_prefix, first_line_in_doc,
previous_line_blank, processed_start_of_line,
- read_yaml_macros_and_links, list_para, output,
- &token, &ptoken, &token_size, TRUE);
+ skip_eol, read_yaml_macros_and_links,
+ list_para, output, &token, &ptoken,
+ &token_size, TRUE);
processed_start_of_line = TRUE;
-
- if (!read_yaml_macros_and_links
- && !(ANY(state, ST_PRE | ST_HEADING)))
- process_code(output, state & ST_CODE);
}
state ^= ST_CODE;
@@ -2280,8 +2334,9 @@ slweb_parse(uint8_t* buffer, FILE* output, BOOL read_yaml_macros_and_links)
/* Output existing text up to __ */
process_text_token(line, link_prefix, first_line_in_doc,
previous_line_blank, processed_start_of_line,
- read_yaml_macros_and_links, list_para, output,
- &token, &ptoken, &token_size, TRUE);
+ skip_eol, read_yaml_macros_and_links,
+ list_para, output, &token, &ptoken,
+ &token_size, TRUE);
processed_start_of_line = TRUE;
if (!read_yaml_macros_and_links
@@ -2302,8 +2357,9 @@ slweb_parse(uint8_t* buffer, FILE* output, BOOL read_yaml_macros_and_links)
/* Output existing text up to _ */
process_text_token(line, link_prefix, first_line_in_doc,
previous_line_blank, processed_start_of_line,
- read_yaml_macros_and_links, list_para, output,
- &token, &ptoken, &token_size, TRUE);
+ skip_eol, read_yaml_macros_and_links,
+ list_para, output, &token, &ptoken,
+ &token_size, TRUE);
processed_start_of_line = TRUE;
if (!read_yaml_macros_and_links
@@ -2392,7 +2448,7 @@ slweb_parse(uint8_t* buffer, FILE* output, BOOL read_yaml_macros_and_links)
{
process_line_start(line, link_prefix, first_line_in_doc,
previous_line_blank, read_yaml_macros_and_links,
- list_para, output, &token, &ptoken);
+ skip_eol, list_para, output, &token, &ptoken);
/* Ignore abbreviations (for now) */
pline = NULL;
@@ -2402,7 +2458,8 @@ slweb_parse(uint8_t* buffer, FILE* output, BOOL read_yaml_macros_and_links)
{
skip_eol = TRUE;
if (!read_yaml_macros_and_links)
- process_horizontal_rule(output);
+ process_horizontal_rule(link_prefix, previous_line_blank,
+ output);
pline = NULL;
}
else if (colno == 1
@@ -2444,8 +2501,9 @@ slweb_parse(uint8_t* buffer, FILE* output, BOOL read_yaml_macros_and_links)
/* Output existing text up to * */
process_text_token(line, link_prefix, first_line_in_doc,
previous_line_blank, processed_start_of_line,
- read_yaml_macros_and_links, list_para, output,
- &token, &ptoken, &token_size, TRUE);
+ skip_eol, read_yaml_macros_and_links,
+ list_para, output, &token, &ptoken,
+ &token_size, TRUE);
processed_start_of_line = TRUE;
if (!read_yaml_macros_and_links
@@ -2467,8 +2525,9 @@ slweb_parse(uint8_t* buffer, FILE* output, BOOL read_yaml_macros_and_links)
/* Output existing text up to * */
process_text_token(line, link_prefix, first_line_in_doc,
previous_line_blank, processed_start_of_line,
- read_yaml_macros_and_links, list_para, output,
- &token, &ptoken, &token_size, TRUE);
+ skip_eol, read_yaml_macros_and_links,
+ list_para, output, &token, &ptoken,
+ &token_size, TRUE);
processed_start_of_line = TRUE;
if (!read_yaml_macros_and_links
@@ -2509,8 +2568,9 @@ slweb_parse(uint8_t* buffer, FILE* output, BOOL read_yaml_macros_and_links)
list_para = TRUE;
process_text_token(line, link_prefix, first_line_in_doc,
previous_line_blank, processed_start_of_line,
- read_yaml_macros_and_links, list_para, output,
- &token, &ptoken, &token_size, TRUE);
+ skip_eol, read_yaml_macros_and_links,
+ list_para, output, &token, &ptoken,
+ &token_size, TRUE);
processed_start_of_line = TRUE;
while (*pline == ' ' || *pline == '\t')
{
@@ -2592,8 +2652,9 @@ slweb_parse(uint8_t* buffer, FILE* output, BOOL read_yaml_macros_and_links)
/* Output existing text up to { */
process_text_token(line, link_prefix, first_line_in_doc,
previous_line_blank, processed_start_of_line,
- read_yaml_macros_and_links, list_para, output,
- &token, &ptoken, &token_size, FALSE);
+ skip_eol, read_yaml_macros_and_links,
+ list_para, output, &token, &ptoken,
+ &token_size, TRUE);
processed_start_of_line = TRUE;
}
@@ -2644,7 +2705,7 @@ slweb_parse(uint8_t* buffer, FILE* output, BOOL read_yaml_macros_and_links)
state &= ~ST_TAG;
*ptoken = 0;
- process_tag(token, link_prefix, output,
+ process_tag(token, link_prefix, output, previous_line_blank,
read_yaml_macros_and_links, &skip_eol, end_tag);
RESET_TOKEN(token, ptoken, token_size)
@@ -2673,42 +2734,20 @@ slweb_parse(uint8_t* buffer, FILE* output, BOOL read_yaml_macros_and_links)
if (u8_strlen(pline) > 1 && *(pline+1) == '|')
{
- /* Handle || within footnotes, headings and link text specially */
- if (ANY(state, ST_INLINE_FOOTNOTE | ST_HEADING
+ if (!ANY(state, ST_INLINE_FOOTNOTE | ST_HEADING
| ST_FOOTNOTE_TEXT | ST_LINK))
{
- uint8_t* tag = state & ST_KBD
- ? (uint8_t*)"</kbd>"
- : (uint8_t*)"<kbd>";
- size_t tag_len = u8_strlen(tag);
- size_t token_len = 0;
-
- *ptoken = 0;
- token_len = u8_strlen(token);
-
- if (token_len + tag_len < BUFSIZE)
- {
- u8_strncat(token, tag, BUFSIZE-token_len-1);
- ptoken += tag_len;
- }
- }
- else
- {
/* Output existing text up to || */
process_text_token(line, link_prefix, first_line_in_doc,
previous_line_blank, processed_start_of_line,
- read_yaml_macros_and_links, list_para, output,
- &token, &ptoken, &token_size, TRUE);
+ skip_eol, read_yaml_macros_and_links,
+ list_para, output, &token, &ptoken,
+ &token_size, TRUE);
processed_start_of_line = TRUE;
-
- if (!read_yaml_macros_and_links
- && !(ANY(state, ST_PRE | ST_CODE | ST_HEADING)))
- {
- state ^= ST_KBD;
- process_kbd(output, !(state & ST_KBD));
- }
}
+ state ^= ST_KBD;
+
pline += 2;
colno += 2;
}
@@ -2851,20 +2890,6 @@ slweb_parse(uint8_t* buffer, FILE* output, BOOL read_yaml_macros_and_links)
break;
}
- /*
- *if (ANY(state, ST_CODE | ST_KBD | ST_PRE))
- *{
- * size_t lt_len = u8_strlen((uint8_t*)"<");
- * *ptoken = 0;
- * u8_strncat(token, (uint8_t*)"<",
- * BUFSIZE-(ptoken-token)-1);
- * ptoken += lt_len;
- * pline++;
- * colno++;
- * break;
- *}
- */
-
state |= ST_HTML_TAG;
/*CHECKCOPY(token, ptoken, token_size, pline)*/
pline++;
@@ -2931,8 +2956,9 @@ slweb_parse(uint8_t* buffer, FILE* output, BOOL read_yaml_macros_and_links)
*ptoken = 0;
process_text_token(line, link_prefix, first_line_in_doc,
previous_line_blank, processed_start_of_line,
- read_yaml_macros_and_links, list_para, output,
- &token, &ptoken, &token_size, FALSE);
+ skip_eol, read_yaml_macros_and_links,
+ list_para, output, &token, &ptoken,
+ &token_size, TRUE);
processed_start_of_line = TRUE;
RESET_TOKEN(token, ptoken, token_size)
@@ -2998,11 +3024,7 @@ slweb_parse(uint8_t* buffer, FILE* output, BOOL read_yaml_macros_and_links)
if (state & ST_FOOTNOTE_TEXT)
{
if (!read_yaml_macros_and_links && (state & ST_PARA_OPEN))
- {
- print_output(output, "\n\n");
- output_firstcol = TRUE;
- process_paralinks(link_prefix, output);
- }
+ process_para_end(link_prefix, previous_line_blank, output);
state &= ~(ST_FOOTNOTE_TEXT | ST_PARA_OPEN);
}
@@ -3015,8 +3037,9 @@ slweb_parse(uint8_t* buffer, FILE* output, BOOL read_yaml_macros_and_links)
*ptoken = 0;
process_text_token(line, link_prefix, first_line_in_doc,
previous_line_blank, processed_start_of_line,
- read_yaml_macros_and_links, list_para, output,
- &token, &ptoken, &token_size, TRUE);
+ skip_eol, read_yaml_macros_and_links,
+ list_para, output, &token, &ptoken,
+ &token_size, TRUE);
}
processed_start_of_line = TRUE;
@@ -3033,8 +3056,9 @@ slweb_parse(uint8_t* buffer, FILE* output, BOOL read_yaml_macros_and_links)
*ptoken = 0;
process_text_token(line, link_prefix, first_line_in_doc,
previous_line_blank, processed_start_of_line,
- read_yaml_macros_and_links, list_para, output,
- &token, &ptoken, &token_size, TRUE);
+ skip_eol, read_yaml_macros_and_links,
+ list_para, output, &token, &ptoken,
+ &token_size, TRUE);
}
processed_start_of_line = TRUE;
@@ -3060,19 +3084,6 @@ slweb_parse(uint8_t* buffer, FILE* output, BOOL read_yaml_macros_and_links)
}
else if (state & ST_LINK)
{
- uint8_t* tag = (uint8_t*)"<span>";
- size_t tag_len = u8_strlen(tag);
- size_t token_len = 0;
-
- *ptoken = 0;
- token_len = u8_strlen(token);
-
- if (token_len + tag_len < BUFSIZE)
- {
- u8_strncat(token, tag, BUFSIZE-token_len-1);
- ptoken += tag_len;
- }
-
state |= ST_LINK_SPAN;
pline++;
}
@@ -3098,23 +3109,7 @@ slweb_parse(uint8_t* buffer, FILE* output, BOOL read_yaml_macros_and_links)
if (state & ST_LINK_SPAN)
{
- if (u8_strlen(pline) > 1
- && *(pline+1) == ']')
- {
- uint8_t* tag = (uint8_t*)"</span>";
- size_t tag_len = u8_strlen(tag);
- size_t token_len = 0;
-
- *ptoken = 0;
- token_len = u8_strlen(token);
-
- if (token_len + tag_len + 1 < BUFSIZE)
- {
- u8_strncat(token, tag, BUFSIZE-token_len-1);
- ptoken += tag_len;
- }
- state &= ~ST_LINK_SPAN;
- }
+ state &= ~ST_LINK_SPAN;
pline++;
}
else if (ANY(state, ST_LINK_SECOND_ARG | ST_IMAGE_SECOND_ARG))
@@ -3132,11 +3127,12 @@ slweb_parse(uint8_t* buffer, FILE* output, BOOL read_yaml_macros_and_links)
pparalinks->value_size = u8_strlen(link_text)+1;
pparalinks->value = u8_strdup(link_text);
- if (output_firstcol
+ if (output_firstcol
&& ((*(line+line_len-1) == ']')
|| *(line+line_len-1) == ')'))
{
skip_eol = TRUE;
+ skip_para_end = TRUE;
pline = NULL;
}
else
@@ -3378,8 +3374,9 @@ slweb_parse(uint8_t* buffer, FILE* output, BOOL read_yaml_macros_and_links)
*ptoken = 0;
process_text_token(line, link_prefix, first_line_in_doc,
previous_line_blank, processed_start_of_line,
- read_yaml_macros_and_links, list_para, output,
- &token, &ptoken, &token_size, TRUE);
+ skip_eol, read_yaml_macros_and_links,
+ list_para, output, &token, &ptoken,
+ &token_size, TRUE);
processed_start_of_line = TRUE;
RESET_TOKEN(token, ptoken, token_size)
@@ -3454,8 +3451,9 @@ slweb_parse(uint8_t* buffer, FILE* output, BOOL read_yaml_macros_and_links)
/* Output existing text up to $$ */
process_text_token(line, link_prefix, first_line_in_doc,
previous_line_blank, processed_start_of_line,
- read_yaml_macros_and_links, list_para, output,
- &token, &ptoken, &token_size, TRUE);
+ skip_eol, read_yaml_macros_and_links,
+ list_para, output, &token, &ptoken,
+ &token_size, TRUE);
}
processed_start_of_line = TRUE;
@@ -3488,8 +3486,9 @@ slweb_parse(uint8_t* buffer, FILE* output, BOOL read_yaml_macros_and_links)
/* Output existing text up to $ */
process_text_token(line, link_prefix, first_line_in_doc,
previous_line_blank, processed_start_of_line,
- read_yaml_macros_and_links, list_para, output,
- &token, &ptoken, &token_size, TRUE);
+ skip_eol, read_yaml_macros_and_links,
+ list_para, output, &token, &ptoken,
+ &token_size, TRUE);
}
processed_start_of_line = TRUE;
@@ -3690,30 +3689,15 @@ slweb_parse(uint8_t* buffer, FILE* output, BOOL read_yaml_macros_and_links)
| ST_IMAGE | ST_LINK))
process_text_token(line, link_prefix, first_line_in_doc,
previous_line_blank, processed_start_of_line,
- read_yaml_macros_and_links, list_para, output,
- &token, &ptoken, &token_size, TRUE);
+ skip_eol, read_yaml_macros_and_links,
+ list_para, output, &token, &ptoken,
+ &token_size, TRUE);
}
if (!ANY(state, ST_PRE | ST_IMAGE_SECOND_ARG | ST_LINK_SECOND_ARG
| ST_LINK_SECOND_ARG_END)
- && (!*pbuffer || *pbuffer == '\n'))
+ && (!*pbuffer || (*pbuffer == '\n')))
{
- if (state & ST_PARA_OPEN)
- {
- if (!read_yaml_macros_and_links)
- {
- print_output(output, "%s",
- ((line_len == 0) || previous_line_blank)
- && output_firstcol
- ? "" : "\n");
- output_firstcol = TRUE;
- process_paralinks(link_prefix, output);
- }
- if (state & ST_LIST)
- skip_eol = TRUE;
- state &= ~ST_PARA_OPEN;
- }
-
if (!*pbuffer && (state & ST_LIST))
{
if (!read_yaml_macros_and_links)
@@ -3734,6 +3718,21 @@ slweb_parse(uint8_t* buffer, FILE* output, BOOL read_yaml_macros_and_links)
state &= ~ST_NUMLIST;
}
+ if (state & ST_PARA_OPEN)
+ {
+ /*
+ *if (skip_para_end) print_output(output, "SPE");
+ *else print_output(output, "!SPE!");
+ */
+ if (!read_yaml_macros_and_links)
+ process_para_end(link_prefix, previous_line_blank, output);
+
+ if (state & ST_LIST)
+ skip_eol = TRUE;
+
+ state &= ~ST_PARA_OPEN;
+ }
+
if (!*pbuffer && (state & ST_FOOTNOTE_TEXT))
{
state &= ~ST_FOOTNOTE_TEXT;
@@ -3809,14 +3808,23 @@ slweb_parse(uint8_t* buffer, FILE* output, BOOL read_yaml_macros_and_links)
first_line_in_doc = FALSE;
skip_change_first_line_in_doc = FALSE;
- if (!previous_line_blank && (line_len == 0))
+ if (line_len == 0)
{
- previous_line_blank = TRUE;
- if (!read_yaml_macros_and_links)
- print_output(output, "\n");
+ if (!previous_line_blank)
+ {
+ if (!read_yaml_macros_and_links
+ && (state & ST_PARA_OPEN))
+ {
+ if (state & ST_LIST)
+ process_list_item_end(link_prefix, output);
+ else
+ process_para_end(link_prefix, previous_line_blank, output);
+ }
+ previous_line_blank = TRUE;
+ }
}
else
- previous_line_blank = TRUE;
+ previous_line_blank = FALSE;
/* Lasts until the end of line */
state &= ~(ST_YAML_VAL | ST_IMAGE_SECOND_ARG | ST_LINK_SECOND_ARG
@@ -3824,12 +3832,14 @@ slweb_parse(uint8_t* buffer, FILE* output, BOOL read_yaml_macros_and_links)
}
while (pbuffer && *pbuffer);
- if (!read_yaml_macros_and_links
- && (footnote_count > 0 || inline_footnote_count > 0))
- end_footnotes(output, link_prefix, add_footnote_div);
-
if (!read_yaml_macros_and_links)
{
+ if (footnote_count > 0 || inline_footnote_count > 0)
+ end_footnotes(output, link_prefix, add_footnote_div, previous_line_blank);
+
+ if (paralinks_count > 0)
+ process_paralinks(link_prefix, output);
+
print_output(output, "\n");
output_firstcol = TRUE;
}
@@ -3874,6 +3884,14 @@ main(int argc, char** argv)
if (result)
return result;
}
+ else if (startswith(arg, "global-link-prefix"))
+ {
+ arg += strlen("global-link-prefix");
+ result = set_global_link_prefix(arg, &global_link_prefix,
+ &global_link_prefix_size);
+ if (result)
+ return result;
+ }
else if (!strcmp(arg, "help"))
return usage();
else
@@ -3889,6 +3907,9 @@ main(int argc, char** argv)
case 'd':
cmd = CMD_BASEDIR;
break;
+ case 'p':
+ cmd = CMD_LINK_PREFIX;
+ break;
case 'h':
return usage();
break;
@@ -3911,6 +3932,13 @@ main(int argc, char** argv)
if (result)
return result;
}
+ else if (cmd == CMD_LINK_PREFIX)
+ {
+ result = set_global_link_prefix(arg, &global_link_prefix,
+ &global_link_prefix_size);
+ if (result)
+ return result;
+ }
else
input_filename = arg;
cmd = CMD_NONE;