Improve POD generated from standalone Markdown Create a name and description section.
Project: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/repo Commit: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/commit/07f37d68 Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/07f37d68 Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/07f37d68 Branch: refs/heads/master Commit: 07f37d687547c582d3e7b03879df8be2441a4a01 Parents: 9964b34 Author: Nick Wellnhofer <wellnho...@aevum.de> Authored: Tue Feb 23 12:23:17 2016 +0100 Committer: Nick Wellnhofer <wellnho...@aevum.de> Committed: Tue Feb 23 13:25:53 2016 +0100 ---------------------------------------------------------------------- compiler/src/CFCPerl.c | 7 ++++--- compiler/src/CFCPerlPod.c | 42 ++++++++++++++++++++++++++++++++++++++++++ compiler/src/CFCPerlPod.h | 18 ++++++++++++++++++ 3 files changed, 64 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/07f37d68/compiler/src/CFCPerl.c ---------------------------------------------------------------------- diff --git a/compiler/src/CFCPerl.c b/compiler/src/CFCPerl.c index f19af61..056afaf 100644 --- a/compiler/src/CFCPerl.c +++ b/compiler/src/CFCPerl.c @@ -242,8 +242,10 @@ S_write_standalone_pod(CFCPerl *self) { for (size_t i = 0; i < num_pod_files; i++) { CFCDocument *doc = docs[i]; + const char *path_part = CFCDocument_get_path_part(doc); + char *module = CFCUtil_global_replace(path_part, CHY_DIR_SEP, "::"); char *md = CFCDocument_get_contents(doc); - char *raw_pod = CFCPerlPod_md_to_pod(md, NULL, 1); + char *raw_pod = CFCPerlPod_md_doc_to_pod(module, md); const char *pattern = "%s" @@ -251,12 +253,10 @@ S_write_standalone_pod(CFCPerl *self) { "=encoding utf8\n" "\n" "%s" - "\n" "%s"; char *pod = CFCUtil_sprintf(pattern, self->pod_header, raw_pod, self->pod_footer); - const char *path_part = CFCDocument_get_path_part(doc); char *pod_path = CFCUtil_sprintf("%s" CHY_DIR_SEP "%s.pod", self->lib_dir, path_part); @@ -265,6 +265,7 @@ S_write_standalone_pod(CFCPerl *self) { FREEMEM(raw_pod); FREEMEM(md); + FREEMEM(module); } pod_files[num_pod_files].contents = NULL; http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/07f37d68/compiler/src/CFCPerlPod.c ---------------------------------------------------------------------- diff --git a/compiler/src/CFCPerlPod.c b/compiler/src/CFCPerlPod.c index 996b4c2..409dc4a 100644 --- a/compiler/src/CFCPerlPod.c +++ b/compiler/src/CFCPerlPod.c @@ -578,6 +578,48 @@ S_camel_to_lower(const char *camel) { } char* +CFCPerlPod_md_doc_to_pod(const char *module, const char *md) { + int options = CMARK_OPT_NORMALIZE + | CMARK_OPT_SMART + | CMARK_OPT_VALIDATE_UTF8 + | CMARK_OPT_SAFE; + cmark_node *doc = cmark_parse_document(md, strlen(md), options); + cmark_node *maybe_header = cmark_node_first_child(doc); + char *name; + char *desc; + + if (maybe_header + && cmark_node_get_type(maybe_header) == CMARK_NODE_HEADER + ) { + cmark_node *header_child = cmark_node_first_child(maybe_header); + char *short_desc = S_nodes_to_pod(header_child, NULL, 1); + name = CFCUtil_sprintf("%s - %s", module, short_desc); + + cmark_node *remaining = cmark_node_next(maybe_header); + desc = S_nodes_to_pod(remaining, NULL, 1); + } + else { + // No header found. + name = CFCUtil_strdup(module); + desc = S_node_to_pod(doc, NULL, 1); + } + + const char *pattern = + "=head1 NAME\n" + "\n" + "%s\n" + "\n" + "=head1 DESCRIPTION\n" + "\n" + "%s"; + char *retval = CFCUtil_sprintf(pattern, name, desc); + + FREEMEM(name); + FREEMEM(desc); + return retval; +} + +char* CFCPerlPod_md_to_pod(const char *md, CFCClass *klass, int header_level) { int options = CMARK_OPT_NORMALIZE | CMARK_OPT_SMART http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/07f37d68/compiler/src/CFCPerlPod.h ---------------------------------------------------------------------- diff --git a/compiler/src/CFCPerlPod.h b/compiler/src/CFCPerlPod.h index 4d4a175..c42a34d 100644 --- a/compiler/src/CFCPerlPod.h +++ b/compiler/src/CFCPerlPod.h @@ -98,6 +98,24 @@ CFCPerlPod_set_description(CFCPerlPod *self, const char *description); const char* CFCPerlPod_get_description(CFCPerlPod *self); +/** Convert a standalone Markdown document to POD. The name section is + * created from the module name and the heading at the beginning of the + * Markdown text. The rest of the Markdown text goes into the description + * section. + * + * @param module The module name used in the name section. + * @param md The Markdown text. + */ +char* +CFCPerlPod_md_doc_to_pod(const char *module, const char *md); + +/** Convert Markdown text to POD. + * + * @param md Markdown text. + * @param klass The current class, used for links. Can be NULL. + * @param header_level The POD header level corresponding to top-level + * Markdown headers. + */ char* CFCPerlPod_md_to_pod(const char *md, struct CFCClass *klass, int header_level);