Expand C recipes.

This commit is contained in:
Joshua Bemenderfer 2023-02-14 17:20:21 -05:00
parent 7dd718691b
commit 91ca89f158
3 changed files with 78 additions and 6 deletions

View File

@ -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.<br/>
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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
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

View File

@ -15,5 +15,7 @@ module.exports = async (doc, rootLevel) => {
else node.text = await contentAsText(doc, rootLevel, true)
}
node.text = node.text.trimEnd()
return node
}

View File

@ -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
}