Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions lib/gis/parser_md.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,24 @@

#include "parser_local_proto.h"

/*!
\brief Print a string as the content of a double-quoted YAML scalar.

Escapes the characters that are special inside a double-quoted YAML
scalar (backslash and double quote). Quoting the value lets it contain
YAML-significant sequences such as ": " without breaking the parser.

\param str string to print (escaped) to stdout
*/
static void print_yaml_escaped(const char *str)
{
for (const char *p = str; *p; p++) {
if (*p == '\\' || *p == '"')
fprintf(stdout, "\\");
fprintf(stdout, "%c", *p);
}
}

/*!
\brief Print module usage description in Markdown format.
*/
Expand All @@ -32,14 +50,14 @@ void G__usage_markdown(void)
/* print metadata used by man/build*.py */
fprintf(stdout, "---\n");
fprintf(stdout, "name: %s\n", st->pgm_name);
fprintf(stdout, "description: ");
fprintf(stdout, "description: \"");
if (st->module_info.label)
fprintf(stdout, "%s", st->module_info.label);
print_yaml_escaped(st->module_info.label);
if (st->module_info.label && st->module_info.description)
fprintf(stdout, " ");
if (st->module_info.description)
fprintf(stdout, "%s", st->module_info.description);
fprintf(stdout, "\n");
print_yaml_escaped(st->module_info.description);
fprintf(stdout, "\"\n");
fprintf(stdout, "keywords: [ ");
G__print_keywords(stdout, NULL, FALSE);
fprintf(stdout, " ]");
Expand Down
17 changes: 16 additions & 1 deletion man/build_md.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import re
import string

# File template pieces follow
Expand Down Expand Up @@ -78,6 +79,20 @@
############################################################################


def unquote_yaml(text):
"""Decode the description value from --md-description frontmatter.

The value is emitted as a double-quoted YAML scalar so it can contain
YAML-significant sequences such as ": ". This strips the surrounding
quotes and undoes the backslash escaping of `"` and `\\`. A value that
is not quoted is returned unchanged.
"""
text = text.strip()
if len(text) >= 2 and text.startswith('"') and text.endswith('"'):
return re.sub(r"\\(.)", r"\1", text[1:-1])
return text


def get_desc(cmd):
desc = ""
with open(cmd) as f:
Expand All @@ -86,7 +101,7 @@ def get_desc(cmd):
if not line:
return desc
if "description:" in line:
desc = line.split(":", 1)[1].strip()
desc = unquote_yaml(line.split(":", 1)[1])
break

return desc
Expand Down
3 changes: 2 additions & 1 deletion man/build_topics.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def build_topics(ext):
headertopics_tmpl,
man_dir,
moduletopics_tmpl,
unquote_yaml,
)

keywords = {}
Expand Down Expand Up @@ -86,7 +87,7 @@ def build_topics(ext):
key = keys[1] # Second keyword is topic.
match = re.match(r"description:\s*(.*)\s*", line)
if match:
text = match.group(1)
text = unquote_yaml(match.group(1))
if not text:
print(f"Warning: Empty tile in {fname}", file=sys.stderr)
break
Expand Down
Loading