From 91ca89f158286e098cde94b9ac4ae7a109b1ee7c Mon Sep 17 00:00:00 2001 From: Joshua Bemenderfer Date: Tue, 14 Feb 2023 17:20:21 -0500 Subject: [PATCH] Expand C recipes. --- docs/src/docs/c/index.tce | 81 ++++++++++++++++++++++++++++-- docs/src/parser/nodes/CodeBlock.js | 2 + docs/src/parser/page.js | 1 - 3 files changed, 78 insertions(+), 6 deletions(-) diff --git a/docs/src/docs/c/index.tce b/docs/src/docs/c/index.tce index edef99a..1777e8a 100644 --- a/docs/src/docs/c/index.tce +++ b/docs/src/docs/c/index.tce @@ -18,25 +18,96 @@ Section light Markdown Documentation is available for the following languages: - - [C](/docs/c/) - 1% Complete + - [C](/docs/c/) - 10% Complete - [JavaScript](/docs/javascript/) - 0% Complete - [Python](/docs/python/) - 0% Complete Heading 2 Getting Started class mt-12 mb-6 - Markdown The core terrace parser is distributed as a single-header C library.
To use it, download [parser.h](https://git.thederf.com/thederf/Terrace/src/branch/main/packages/c/parser.h) and include in your project tree. - CodeBlock c #include "parser.h" - Markdown - Heading 2 Recipes class mt-12 + Heading 3 Parse a single line + Markdown + Parses a single line into `line_data`, the prints the information from `line_data`. + CodeBlock c + #include "parser.h" + + int main(int argc, char *argv[]) { + char* line = "example line"; + // Create the line_data struct + terrace_linedata_t line_data; + // Set the indent character to a space + line_data.indent = ' '; + // Populates line_data level, offsetHead, and offsetTail from line + terrace_parse_line(line, &line_data); + + printf( + "level %u | indent %c | offsetHead %u | offsetTail %u\n", + line_data.level, + line_data.indent, + line_data.offsetHead, + line_data.offsetTail + ); + + return 0; + } + + Heading 3 Parse all lines from stdin + Markdown + Reads lines from stdin one-by-one and prints each line's `line_data`. + CodeBlock c + #include "parser.h" + // Depends on several cstdlib functions + #include + #include + #include + #include + + int main(int argc, char *argv[]) { + // Pointer to start of line + char *line = NULL; + // Initial size of the buffer to read into + // getline() will resize as needed + size_t bufsize = 128; + // How many characters have been read + ssize_t chars_read = 0; + + // Create the line_data struct + terrace_linedata_t line_data; + // Set the indent character to a space + line_data.indent = ' '; + + while (chars_read = getline(&line, &bufsize, stdin)) { + // If chars_read is -1, we've reached end of file. + if (chars_read == -1) break; + // getline returns lines with a trailing newline + // terrace_parse_line expects no trailing newline + // strip it off using strtok() + // (An odd solution, probably leaks memory) + char *terrace_line = strtok(line, "\n"); + terrace_parse_line(terrace_line, &line_data); + + printf( + "level %u | indent %c | offsetHead %u | offsetTail %u\n", + line_data.level, + line_data.indent, + line_data.offsetHead, + line_data.offsetTail + ); + }; + + // Free the buffer allocated by getline(). + free(line); + } + + Heading 2 Core API class mt-12 diff --git a/docs/src/parser/nodes/CodeBlock.js b/docs/src/parser/nodes/CodeBlock.js index 2a4d22f..d5253e9 100644 --- a/docs/src/parser/nodes/CodeBlock.js +++ b/docs/src/parser/nodes/CodeBlock.js @@ -15,5 +15,7 @@ module.exports = async (doc, rootLevel) => { else node.text = await contentAsText(doc, rootLevel, true) } + node.text = node.text.trimEnd() + return node } diff --git a/docs/src/parser/page.js b/docs/src/parser/page.js index df0f5af..3e69ec6 100644 --- a/docs/src/parser/page.js +++ b/docs/src/parser/page.js @@ -41,7 +41,6 @@ module.exports = async function(doc) { }) pageData.headings = pageData.headings.filter(h => h.level === 2) - console.dir(pageData.headings, { depth: null }) return pageData }