Skip to content

Commit

Permalink
Update markdown output to support code classes and link titles.
Browse files Browse the repository at this point in the history
Fix typo in documentation.
  • Loading branch information
michaelrsweet committed Aug 19, 2019
1 parent 6af362f commit e659072
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 16 deletions.
4 changes: 2 additions & 2 deletions DOCUMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: How to Use the codedoc Utility
author: Michael R Sweet
copyright: Copyright © 2003-2019 by Michael R Sweet
version: 3.1
version: 3.2
...

Introduction
Expand Down Expand Up @@ -96,7 +96,7 @@ generated documentation:
file can be markdown, HTML, or man source
- `--section "{name,number}"`: Sets the section/category name (HTML and EPUB
output) or section number (man page output)
- `--title "title text'`; Sets the title text
- `--title "title text'`: Sets the title text


Annotating Your C/C++ Code
Expand Down
62 changes: 48 additions & 14 deletions codedoc.c
Original file line number Diff line number Diff line change
Expand Up @@ -2016,7 +2016,11 @@ markdown_write_block(FILE *out, /* I - Output file */
break;

case MMD_TYPE_CODE_BLOCK :
fputs(" <pre><code>", out);
if ((class_name = mmdGetExtra(parent)) != NULL)
fprintf(out, " <pre><code class=\"language-%s\">", class_name);
else
fputs(" <pre><code>", out);

for (node = mmdGetFirstChild(parent); node; node = mmdGetNextSibling(node))
write_string(out, mmdGetText(node), mode);
fputs("</code></pre>\n", out);
Expand Down Expand Up @@ -2118,18 +2122,22 @@ markdown_write_leaf(FILE *out, /* I - Output file */
mmd_t *node, /* I - Node to write */
int mode) /* I - Output mode */
{
mmd_type_t type, /* Current leaf node type */
prev_type, /* Previous leaf node type */
next_type; /* Next leaf node type */
const char *text, /* Text to write */
*url; /* URL to write */


type = mmdGetType(node);
text = mmdGetText(node);
url = mmdGetURL(node);

if (mode == OUTPUT_MAN)
{
const char *suffix = NULL; /* Trailing string */

switch (mmdGetType(node))
switch (type)
{
case MMD_TYPE_EMPHASIZED_TEXT :
if (mmdGetWhitespace(node))
Expand Down Expand Up @@ -2176,7 +2184,7 @@ markdown_write_leaf(FILE *out, /* I - Output file */
if (mmdGetWhitespace(node))
fputc(' ', out);

switch (mmdGetType(node))
switch (type)
{
case MMD_TYPE_EMPHASIZED_TEXT :
element = "em";
Expand Down Expand Up @@ -2210,10 +2218,13 @@ markdown_write_leaf(FILE *out, /* I - Output file */
return;

case MMD_TYPE_HARD_BREAK :
if (mode == OUTPUT_EPUB)
fputs("<br />\n", out);
else
fputs("<br>\n", out);
if (mmdGetType(mmdGetParent(node)) < MMD_TYPE_HEADING_1 || mmdGetType(mmdGetParent(node)) > MMD_TYPE_HEADING_6)
{
if (mode == OUTPUT_EPUB)
fputs("<br />\n", out);
else
fputs("<br>\n", out);
}
return;

case MMD_TYPE_SOFT_BREAK :
Expand All @@ -2231,24 +2242,47 @@ markdown_write_leaf(FILE *out, /* I - Output file */
break;
}

prev_type = mmdGetType(mmdGetPrevSibling(node));
next_type = mmdGetType(mmdGetNextSibling(node));

if (url)
{
if (!strcmp(url, "@"))
fprintf(out, "<a href=\"#%s\">", markdown_anchor(text));
else
fprintf(out, "<a href=\"%s\">", url);
const char *prev_url = mmdGetURL(mmdGetPrevSibling(node));
const char *title = mmdGetExtra(node);

if (!prev_url || strcmp(prev_url, url))
{
if (!strcmp(url, "@"))
fprintf(out, "<a href=\"#%s\"", markdown_anchor(text));
else
fprintf(out, "<a href=\"%s\"", url);

if (title)
{
fputs(" title=\"", out);
write_string(out, title, mode);
fputs("\">", out);
}
else
putc('>', out);
}
}

if (element)
if (element && prev_type != type)
fprintf(out, "<%s>", element);

write_string(out, text, mode);

if (element)
if (element && next_type != type)
fprintf(out, "</%s>", element);

if (url)
fputs("</a>", out);
{
const char *next_url = mmdGetURL(mmdGetNextSibling(node));

if (!next_url || strcmp(next_url, url))
fputs("</a>", out);
}
}
}

Expand Down

0 comments on commit e659072

Please sign in to comment.