8000 miniaudio/tools/madoc/madoc.c at dev-0.12 · Zaczero/miniaudio · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
2017 lines (1619 loc) · 65.6 KB

madoc.c

File metadata and controls

2017 lines (1619 loc) · 65.6 KB
/*
This is very rough and built specifically for miniaudio. Don't get clever and try using this for
your own project because it will most likely not work.
*/
#include "../../../fs/fs.c"
#define C89STR_IMPLEMENTATION
#include "../../../c89str/c89str.h"
/*
This runs in two phases. The first phase generates a webplate-compatible template site. The second phase
uses webplate to actually generate the actual website.
*/
#include "../../../webplate/source/libwebplate.c"
/* The output directory, relative to the tools/build/_bin directory. */
#define WEBSITE_DOCS_FOLDER "website/docs"
#define WEBSITE_DOCS_MANUAL_FOLDER WEBSITE_DOCS_FOLDER"/manual"
#define WEBSITE_DOCS_EXAMPLES_FOLDER WEBSITE_DOCS_FOLDER"/examples"
#define WEBSITE_DOCS_API_FOLDER WEBSITE_DOCS_FOLDER"/api"
#define EXAMPLES_FOLDER "examples"
c89str load_file(const char* pFilePath)
{
fs_result resultFS;
size_t fileSize;
char* pFileData;
c89str str;
resultFS = fs_file_open_and_read(NULL, pFilePath, FS_FORMAT_TEXT, (void**)&pFileData, &fileSize);
if (resultFS != FS_SUCCESS) {
printf("Failed to open %s: %s.\n", pFilePath, fs_result_description(resultFS));
return NULL;
}
/* We have the file data, but now we need to convert it to a dynamic string so we can manipulate it later. */
str = c89str_newn(NULL, pFileData, fileSize);
fs_free(pFileData, NULL);
return str;
}
int save_file(const char* pFilePath, c89str src)
{
fs_result resultFS;
resultFS = fs_file_open_and_write(NULL, pFilePath, src, c89str_len(src));
if (resultFS != FS_SUCCESS) {
printf("Failed to save %s: %s.\n", pFilePath, fs_result_description(resultFS));
return (int)resultFS;
}
return 0;
}
c89str convert_tabs_to_spaces(c89str str)
{
/* Assume a tab is 4 spaces. */
return c89str_replace_all(str, NULL, "\t", 1, " ", 4);
}
c89str strip_code_block_comments(c89str str)
{
size_t runningOffset = 0;
if (str == NULL) {
return str;
}
/*
Block comments are not recursive which slightly simplifies our algorithm. The algorithm we're using here is not
based on speed and can be made much more efficient. Note that this will stip blocks that contain an opening or
closing block within a string constant. The proper way to do this is to use a proper C parser, but that is more
than what we need right now.
*/
for (;;) {
size_t openingOffset;
size_t closingOffset;
/* Opening. */
if (c89str_find(str + runningOffset, "/*", &openingOffset) != C89STR_SUCCESS) {
break; /* We're done. */
}
openingOffset += runningOffset; /* Normalize the opening offset. */
/* Closing. */
if (c89str_find(str + openingOffset, "*/", &closingOffset) != C89STR_SUCCESS) {
break; /* We're done. */
}
closingOffset += openingOffset; /* Normalize the closing offset. */
closingOffset += 2; /* Make sure to include the closing block itself. */
/* We now have enough information to delete the comment block. */
str = c89str_replace(str, NULL, openingOffset, (closingOffset - openingOffset), "", 0);
if (str == NULL) {
return str;
}
/* We're done with this block so we can now move on to the next. */
runningOffset = openingOffset;
}
return str;
}
c89str strip_code_line_comments(c89str str)
{
if (str == NULL) {
return str;
}
/* TODO: Implement me. */
return str;
}
c89str strip_code_comments(c89str str)
{
return strip_code_line_comments(strip_code_block_comments(str));
}
c89str strip_empty_lines(c89str str)
{
const char* pRunningStr;
c89str newstr = NULL;
size_t lineLen;
size_t nextLineOffset;
if (str == NULL) {
return str;
}
pRunningStr = str;
/* We are going to fully recreate the string line by line. We can allocate enough space from the start. */
newstr = c89str_new_with_cap(NULL, c89str_len(str));
if (newstr == NULL) {
return str;
}
while (pRunningStr[0] != '\0') {
nextLineOffset = c89str_utf8_find_next_line(pRunningStr, (size_t)-1, &lineLen);
if (c89str_is_null_or_whitespace(pRunningStr, lineLen) == C89STR_FALSE) {
newstr = c89str_catn(newstr, NULL, pRunningStr, nextLineOffset);
}
if (nextLineOffset == c89str_npos) {
break;
} else {
pRunningStr += nextLineOffset;
}
}
if (newstr == NULL) {
return str;
}
c89str_delete(str, NULL);
return newstr;
}
c89str strip_trailing_whitespace(c89str str)
{
const char* pRunningStr;
c89str newstr = NULL;
if (str == NULL) {
return str;
}
pRunningStr = str;
/* We are going to fully recreate the string line by line. We can allocate enough space from the start. */
newstr = c89str_new_with_cap(NULL, c89str_len(str));
if (newstr == NULL) {
return str;
}
while (pRunningStr[0] != '\0') {
size_t lineLen;
size_t nextLineOffset;
nextLineOffset = c89str_utf8_find_next_line(pRunningStr, (size_t)-1, &lineLen);
newstr = c89str_catn(newstr, NULL, pRunningStr, c89str_utf8_rtrim_offset(pRunningStr, lineLen));
/* Insert the new line character. */
if (nextLineOffset != c89str_npos) {
newstr = c89str_catn(newstr, NULL, pRunningStr + lineLen, nextLineOffset - lineLen);
} else {
newstr = c89str_catn(newstr, NULL, pRunningStr + lineLen, lineLen);
}
if (nextLineOffset == c89str_npos) {
break;
} else {
pRunningStr += nextLineOffset;
}
}
if (newstr == NULL) {
return str;
}
c89str_delete(str, NULL);
return newstr;
}
c89str strip_whitespace(c89str str)
{
return strip_trailing_whitespace(strip_empty_lines(str));
}
c89str minify_code(c89str str)
{
return convert_tabs_to_spaces(strip_whitespace(strip_code_comments(str)));
}
static int path_remove_extension(char* dst, size_t dstSizeInBytes, const char* src)
{
const char* ext;
if (src == NULL) {
if (dst != NULL && dstSizeInBytes > 0) {
dst[0] = '\0';
}
return -1;
}
ext = fs_path_extension(src, FS_NULL_TERMINATED);
if (ext == NULL || ext[0] == '\0') {
/* No extension. */
if (dst == src) {
return (ext - dst);
} else {
size_t len = strlen(src);
fs_strncpy_s(dst, dstSizeInBytes, src, len);
return (int)len;
}
} else {
/* Have extension. */
size_t dstLen = (size_t)(ext - src - 1); /* -1 for the period. */
if (dst == src) {
dst[dstLen] = '\0';
} else {
fs_strncpy_s(dst, dstSizeInBytes, src, dstLen);
}
return dstLen;
}
}
static int path_append_extension(char* dst, size_t dstSizeInBytes, const char* base, const char* extension)
{
fs_result result = FS_SUCCESS;
size_t baseLength;
size_t extLength;
size_t dstLen;
if (base == NULL) {
base = "";
}
if (extension == NULL) {
extension = "";
}
if (extension[0] == '\0') {
if (dst != NULL) {
if (dst != base) {
fs_strcpy_s(dst, dstSizeInBytes, base);
}
}
return (int)strlen(base);
}
baseLength = strlen(base);
extLength = strlen(extension);
dstLen = baseLength + 1 + extLength;
if (dst != NULL) {
if (dstLen+1 <= dstSizeInBytes) {
if (dst != base) {
fs_strcpy_s(dst + 0, dstSizeInBytes - 0, base);
}
fs_strcpy_s(dst + baseLength, dstSizeInBytes - baseLength, ".");
fs_strcpy_s(dst + baseLength + 1, dstSizeInBytes - baseLength - 1, extension);
}
}
return (int)dstLen;
}
static fs_result fs_rmdir_content(const char* pDirectory)
{
/* We'll use an iterator for this. */
fs_iterator* pIterator;
if (pDirectory == NULL) {
return FS_INVALID_ARGS;
}
pIterator = fs_first(NULL, pDirectory, FS_READ);
while (pIterator != NULL) {
char* pFilePath;
int filePathLen;
/* Get the length first. */
filePathLen = fs_path_append(NULL, 0, pDirectory, FS_NULL_TERMINATED, pIterator->pName, pIterator->nameLen);
if (filePathLen > 0) {
pFilePath = (char*)fs_malloc((size_t)filePathLen + 1, NULL); /* +1 for null terminator. */
if (pFilePath != NULL) {
fs_path_append(pFilePath, (size_t)filePathLen + 1, pDirectory, FS_NULL_TERMINATED, pIterator->pName, pIterator->nameLen);
if (pIterator->info.directory) {
if (pIterator->pName[0] == '.' && pIterator->pName[1] == '\0') {
/* "." - ignore. */
} else if (pIterator->pName[0] == '.' && pIterator->pName[1] == '.' && pIterator->pName[2] == '\0') {
/* ".." - ignore. */
} else {
fs_rmdir_content(pFilePath);
}
}
fs_remove(NULL, pFilePath, 0);
} else {
return FS_OUT_OF_MEMORY;
}
} else {
/* Failed to retrieve the file path length. */
}
pIterator = fs_next(pIterator);
}
fs_free_iterator(pIterator);
return FS_SUCCESS;
}
typedef struct
{
c89str name;
c89str code;
} doc_example;
typedef enum
{
doc_category_home,
doc_category_manual,
doc_category_examples,
doc_category_api
} doc_category;
typedef enum
{
doc_token_type_paragraph,
doc_token_type_code,
doc_token_type_table,
doc_token_type_header,
doc_token_type_list_item
} doc_token_type;
typedef enum
{
doc_lang_none,
doc_lang_c
} doc_lang;
typedef struct
{
const char* pText;
size_t textLen; /* Set to (size_t)-1 for null terminated. */
size_t textOff; /* The cursor. */
doc_token_type token;
const char* pTokenStr;
size_t tokenLen;
size_t headerLevel;
size_t indentation; /* Useful for knowing how to offset code. Code tags can be indented and we want to know how deep the base level of indentation is. */
size_t prevLineOff;
size_t prevLineLen;
doc_lang codeLang;
int isLastListItem;
size_t listItemCounter;
} doc_lexer;
int doc_lexer_init(const char* pText, size_t textLen, doc_lexer* pLexer)
{
if (pText == NULL || pLexer == NULL) {
return EINVAL;
}
C89STR_ZERO_OBJECT(pLexer);
if (textLen == (size_t)-1) {
textLen = c89str_strlen(pText);
}
pLexer->pText = pText;
pLexer->textLen = textLen;
pLexer->textOff = 0;
return 0;
}
static int doc_lexer_is_header_underline(const char* pText, size_t textLen, char ch)
{
size_t i;
if (textLen == 0) {
return 0;
}
for (i = 0; i < textLen; i += 1) {
if (pText[i] == '\0') {
break;
}
if (pText[i] != ch) {
return 0;
}
}
/* Getting here means it's an underline. */
return 1;
}
static size_t doc_lexer_get_header_underline_level(const char* pText, size_t textLen)
{
if (doc_lexer_is_header_underline(pText, textLen, '-')) {
return 2;
}
if (doc_lexer_is_header_underline(pText, textLen, '=')) {
return 1;
}
return 0;
}
int doc_lexer_next(doc_lexer* pLexer)
{
//int result;
const char* txt;
size_t off;
size_t len;
if (pLexer == NULL) {
return EINVAL;
}
txt = pLexer->pText;
off = pLexer->textOff; /* Moves forward. */
len = pLexer->textLen; /* Constant. */
/* We run line-by-line. */
for (;;) {
size_t thisLineLen;
size_t nextLineBeg;
size_t indentation;
size_t headerLevel;
size_t location; /* When searching for a token on a line. */
if (txt[off] == '\0' || len == off) {
/* We're done. The last paragraph needs to be added, if any. */
if (off > pLexer->textOff) {
pLexer->token = doc_token_type_paragraph;
pLexer->pTokenStr = txt + pLexer->textOff;
pLexer->tokenLen = off - pLexer->textOff;
pLexer->textOff = off;
return 0;
} else {
return ENOMEM; /* We're done. */
}
}
/* Grab the next line. */
nextLineBeg = c89str_utf8_find_next_line(txt + off, len - off, &thisLineLen);
/* Check if we have a header underline. If so, we want to end the paragraph, if any, and then return. */
headerLevel = doc_lexer_get_header_underline_level(txt + off, thisLineLen);
if (headerLevel > 0) {
if (pLexer->textOff < pLexer->prevLineOff) {
/* We have a paragraph to post. */
pLexer->token = doc_token_type_paragraph;
pLexer->pTokenStr = txt + pLexer->textOff;
pLexer->tokenLen = pLexer->prevLineOff - pLexer->textOff;
pLexer->textOff = off;
} else {
/* We don't have a paragraph. Just post the header itself. */
pLexer->token = doc_token_type_header;
pLexer->pTokenStr = txt + pLexer->prevLineOff;
pLexer->tokenLen = pLexer->prevLineLen;
pLexer->textOff = off + nextLineBeg;
pLexer->headerLevel = headerLevel;
}
return 0;
}
/* Getting here means it's not a header. */
indentation = c89str_utf8_ltrim_offset(txt + off, len - off);
if (c89str_findn(txt + off, thisLineLen, "```", (size_t)-1, &location) == C89STR_SUCCESS) {
/* The beginning of a code block. If there's a pending paragraph that needs to be posted. */
if (pLexer->textOff < off) {
/* We have a paragraph to post. */
pLexer->token = doc_token_type_paragraph;
pLexer->pTokenStr = txt + pLexer->textOff;
pLexer->tokenLen = off - pLexer->textOff;
pLexer->textOff = off;
} else {
/* We don't have a paragraph. We can now parse the code block. */
size_t tokenOff = off;
size_t tokenLen = 0;
doc_lang lang = doc_lang_none;
if (txt[off + location + 3] == 'c') {
lang = doc_lang_c;
}
nextLineBeg = c89str_utf8_find_next_line(txt + off, len - off, &thisLineLen);
tokenOff = off + nextLineBeg;
tokenLen = 0;
off += nextLineBeg;
/* We now need to keep looping over each line until we find the closing the code block. */
for (;;) {
if (txt[off] == '\0' || off == len) {
break;
}
nextLineBeg = c89str_utf8_find_next_line(txt + off, len - off, &thisLineLen);
if (c89str_findn(txt + off, thisLineLen, "```", (size_t)-1, &location) == FS_SUCCESS) {
/* We found the end of the code block. */
pLexer->token = doc_token_type_code;
pLexer->pTokenStr = txt + tokenOff;
pLexer->tokenLen = tokenLen;
pLexer->textOff = off + nextLineBeg;
pLexer->indentation = indentation;
pLexer->codeLang = lang;
break;
} else {
}
off += nextLineBeg;
tokenLen += nextLineBeg;
}
}
return 0;
}
/* Getting here means it's not a header nor a code block. It might be a table. */
if (c89str_findn(txt + off, thisLineLen, "+--", (size_t)-1, &location) == FS_SUCCESS) {
/* The beginning of a table. The end of the table is the end of the last line that starts with a "+" or "|". */
if (pLexer->textOff < off) {
/* We have a paragraph to post. */
pLexer->token = doc_token_type_paragraph;
pLexer->pTokenStr = txt + pLexer->textOff;
pLexer->tokenLen = off - pLexer->textOff;
pLexer->textOff = off;
} else {
/* We don't have a paragraph. We can now parse the table block. */
size_t tokenOff = off;
size_t tokenLen = 0;
for (;;) {
if (txt[off] == '\0' || off == len) {
break;
}
nextLineBeg = c89str_utf8_find_next_line(txt + off, len - off, &thisLineLen);
if (nextLineBeg < 4 || (txt[off + indentation] != '+' && txt[off + indentation] != '|')) {
/* We found the end of the table. The end of the table is the end of this line. */
pLexer->token = doc_token_type_table;
pLexer->pTokenStr = txt + tokenOff;
pLexer->tokenLen = tokenLen;
pLexer->textOff = off + nextLineBeg;
pLexer->indentation = indentation;
break;
} else {
/* We're still looking at the table. */
}
off += nextLineBeg;
tokenLen += nextLineBeg;
}
}
return 0;
}
/* Check if it's a bullet point. */
if (txt[off + indentation] == '-' || txt[off + indentation] == '*') {
if (pLexer->textOff < off) {
/* There's a pending paragraph to post. */
pLexer->token = doc_token_type_paragraph;
pLexer->pTokenStr = txt + pLexer->textOff;
pLexer->tokenLen = off - pLexer->textOff;
pLexer->textOff = off;
} else {
/* A bullet point can cover multiple lines. The bullet point ends when a line starts with another bullet point or is empty. */
size_t tokenOff = off + indentation + 2;
size_t tokenLen = 0;
//int isLineBlank = 0;
int isNextLineBlank = 0;
off += indentation + 2;
/* Keep going until we find the start of a new bullet point or an empty line. */
for (;;) {
int foundEndOfBulletPoint = 0;
if (txt[off] == '\0' || off == len) {
break; /* At end. */
}
/* Go to the next line. */
nextLineBeg = c89str_utf8_find_next_line(txt + off, len - off, &thisLineLen);
/* Determine whether or not the next line is blank. */
isNextLineBlank = c89str_is_null_or_whitespace(txt + off + nextLineBeg, c89str_utf8_find_next_line(txt + off + nextLineBeg, len - off - nextLineBeg, NULL));
if (isNextLineBlank) {
foundEndOfBulletPoint = 1;
} else {
/* The next line is not blank so now check if it starts with a bullet point. */
size_t lineContentOff = off + nextLineBeg + c89str_utf8_ltrim_offset(txt + off + nextLineBeg, len - off - nextLineBeg);
if (txt[lineContentOff] == '-' || txt[lineContentOff] == '*' || txt[lineContentOff] == '\0') {
foundEndOfBulletPoint = 1;
}
}
if (foundEndOfBulletPoint) {
/* We found the end of the bullet point. Update the lexer and return. */
pLexer->token = doc_token_type_list_item;
pLexer->pTokenStr = txt + tokenOff;
pLexer->tokenLen = tokenLen + thisLineLen;
pLexer->textOff = off + nextLineBeg;
pLexer->indentation = indentation;
/* Reset the list item counter if necessary. */
if (pLexer->isLastListItem) {
pLexer->listItemCounter = 0;
}
pLexer->listItemCounter += 1;
pLexer->isLastListItem = isNextLineBlank;
return 0;
} else {
/* We didn't find the end of the bullet point. Move forward. */
off += indentation + nextLineBeg;
tokenLen += indentation + nextLineBeg;
}
}
}
return 0;
}
/* Not a header underline, code block or table. Assume a paragraph. If we have an empty line it means we can terminate our paragraph. */
if (c89str_is_null_or_whitespace(txt + off, thisLineLen)) {
if (pLexer->textOff < off) {
/* We have a paragraph to post. */
pLexer->token = doc_token_type_paragraph;
pLexer->pTokenStr = txt + pLexer->textOff;
pLexer->tokenLen = off - pLexer->textOff;
pLexer->textOff = off;
if (!c89str_is_null_or_whitespace(pLexer->pTokenStr, pLexer->tokenLen)) {
return 0;
}
}
}
/* We need to keep track of the previous line for the purpose of header underlines. */
pLexer->prevLineOff = off;
pLexer->prevLineLen = thisLineLen;
/* We're done. We can move to the next line now. */
off += nextLineBeg;
}
/*return 0;*/
}
typedef struct
{
c89str title;
c89str bookmark;
size_t level;
} manual_section;
typedef struct
{
struct
{
c89str raw;
size_t sectionCount;
manual_section sections[256];
} manual;
struct
{
size_t count;
doc_example examples[256];
} examples;
} docstate;
c89str extract_manual_section_title(const char* pStr, size_t len, size_t* pLevel)
{
if (pLevel != NULL) {
*pLevel = 0;
}
/* We want to remove the numbers because we have limited horizontal space. */
while (pStr[0] != '\0' && len > 0) {
if ((pStr[0] >= '0' && pStr[0] <= '9') || pStr[0] == '.') {
pStr += 1;
len -= 1;
if (pStr[0] == '.' && pLevel != NULL) {
*pLevel += 1;
}
continue;
} else {
break;
}
}
return c89str_trim(c89str_newn(NULL, pStr, len), NULL);
}
c89str extract_manual_section_bookmark(const char* pStr, size_t len)
{
return c89str_replace_all(extract_manual_section_title(pStr, len, NULL), NULL, " ", 1, "-", 0);
}
int load_manual(c89str miniaudio, docstate* pState)
{
int result;
c89str_lexer lexer;
int commentCounter;
result = c89str_lexer_init(&lexer, miniaudio, c89str_len(miniaudio));
if (result != 0) {
return result;
}
/* The manual will be in the second comment block. The first comment block is the project summary. */
commentCounter = 0;
for (;;) {
result = c89str_lexer_next(&lexer);
if (result != 0) {
return result;
}
if (lexer.token == c89str_token_type_comment) {
commentCounter += 1;
}
if (commentCounter == 2) {
/* We've found the comment block with the content of the programming manual. We just need to remove the enveloping comment tokens. */
c89str_lexer_transform_token(&lexer, &pState->manual.raw, NULL);
if (pState->manual.raw == NULL) {
return ENOMEM;
} else {
break;
}
}
}
/* We need to extract all the sections of the manual for the purpose of the navigation panel. */
{
doc_lexer docLexer;
result = doc_lexer_init(pState->manual.raw, c89str_len(pState->manual.raw), &docLexer);
if (result != 0) {
return ENOMEM;
}
for (;;) {
result = doc_lexer_next(&docLexer);
if (result != 0) {
break; /* We're done. */
}
if (docLexer.token == doc_token_type_header) {
manual_section section;
section.title = extract_manual_section_title(docLexer.pTokenStr, docLexer.tokenLen, &section.level);
section.bookmark = extract_manual_section_bookmark(docLexer.pTokenStr, docLexer.tokenLen);
pState->manual.sections[pState->manual.sectionCount] = section;
pState->manual.sectionCount += 1;
}
}
}
return 0;
}
int load_examples(docstate* pState)
{
fs_result resultFS;
fs_iterator* pIterator;
/* Extract all examples. */
pIterator = fs_first(NULL, EXAMPLES_FOLDER, FS_READ);
while (pIterator != NULL) {
if (pIterator->info.size > 0) { /* Ignore any empty files. Sometimes I'll put placeholder files in the examples folder to keep track of ideas for examples. */
/* We need to get the contents of the file. */
doc_example example;
char* pFileData;
char filePath[4096];
fs_path_append(filePath, sizeof(filePath), EXAMPLES_FOLDER, FS_NULL_TERMINATED, pIterator->pName, pIterator->nameLen);
/* Name. We don't care about the whole file path - just the name part of it. */
example.name = c89str_newn(NULL, pIterator->pName, pIterator->nameLen);
/* File content. */
resultFS = fs_file_open_and_read(NULL, filePath, FS_FORMAT_TEXT, (void**)&pFileData, NULL);
if (resultFS != FS_SUCCESS) {
printf("Failed to load example: %s\n", pIterator->pName);
return -1;
}
example.code = c89str_new(NULL, pFileData);
fs_free(pFileData, NULL);
pState->examples.examples[pState->examples.count] = example;
pState->examples.count += 1;
}
pIterator = fs_next(pIterator);
}
fs_free_iterator(pIterator);
return 0;
}
int load_api(c89str miniaudio, docstate* pState)
{
(void)miniaudio;
(void)pState;
return 0;
}
int load(docstate* pState)
{
int result;
c89str miniaudio;
C89STR_ZERO_OBJECT(pState);
/* Data is extracted from miniaudio.h, so we'll need to get that loaded as a start. */
miniaudio = load_file("miniaudio.h");
if (miniaudio == NULL) {
printf("Failed to load miniaudio.h");
return -1;
}
result = load_manual(miniaudio, pState);
if (result != 0) {
printf("Failed to load manual.");
return result;
}
result = load_examples(pState);
if (result != 0) {
printf("Failed to load examples.");
return result;
}
result = load_api(miniaudio, pState);
if (result != 0) {
printf("Failed to load API.");
return result;
}
/* We're done. */
c89str_delete(miniaudio, NULL);
return 0;
}
c89str raw_to_html(c89str raw);
c89str transform_inline_code(c89str html)
{
size_t offset = 0;
/*
To do this we just need to find the next grave character. Then the content between that and the following grave needs to be wrapped in a <span></span> tag
with a monospace font.
*/
for (;;) {
size_t loc1;
size_t loc2;
c89str inner;
c89str replacement;
if (c89str_find(html + offset, "`", &loc1) != C89STR_SUCCESS) {
break; /* No more occurances. */
}
if (c89str_find(html + offset + loc1 + 1, "`", &loc2) != C89STR_SUCCESS) {
break; /* No more occurances. */
}
loc2 += loc1 + 1; /* Make the closing grave offset relative to "offset" like loc1. */
/* We need a copy of the section inside the graves just in case the replacement operation needs to reallocate the pointer. */
inner = c89str_newn(NULL, html + offset + loc1 + 1, loc2 - loc1 - 1);
if (inner == NULL) {
return NULL; /* Out of memory. */
}
replacement = c89str_newf(NULL, "<span style=\"font-family:monospace;\">%s</span>", inner);
c89str_delete(inner, NULL);
if (replacement == NULL) {
return NULL;
}
/* We now have what we need to replace the segment. */
html = c89str_replace(html, NULL, offset + loc1, loc2 - loc1 + 1, replacement, c89str_len(replacement));
if (html == NULL) {
c89str_delete(replacement, NULL);
return NULL; /* Out of memory. */
}
offset += loc1 + c89str_len(replacement);
c89str_delete(replacement, NULL);
}
return html;
}
c89str transform_urls_by_protocol(c89str html, const char* pProtocol)
{
size_t offset = 0;
size_t protocolLen;
protocolLen = c89str_strlen(pProtocol);
if (protocolLen == 0) {
return html;
}
for (;;) {
size_t loc1;
size_t loc2;
c89str url;
c89str replacement;
if (c89str_find(html + offset, pProtocol, &loc1) != C89STR_SUCCESS) {
break; /* No more occurances. */
}
loc2 = c89str_find_next_whitespace(html + offset + loc1, (size_t)-1, NULL);
if (loc2 != c89str_npos) {
loc2 += loc1;
} else {
loc2 = c89str_len(html) - offset;
}
/* We need a copy of the section inside the graves just in case the replacement operation needs to reallocate the pointer. */
url = c89str_trim(c89str_newn(NULL, html + offset + loc1, loc2 - loc1), NULL);
if (url == NULL) {
return NULL; /* Out of memory. */
}
0