Call into the style code from translate. I think the right styles are being set now. Just need to write the resources to the output file.

This commit is contained in:
Jeremy Rand 2021-04-27 20:26:12 -04:00
parent 2d31af9b45
commit ce6666456d
2 changed files with 94 additions and 49 deletions

View File

@ -14,14 +14,20 @@ h2
-------------------- --------------------
indented code indented code
* list item code
1. number list code
``` ```
fenced code fenced code
* list item code
1. number list code
``` ```
<tag attr='val' attr2="val2"> <tag attr='val' attr2="val2">
> quote > quote
> * list quote
> 1. number list quote
* list item * list item
1. list item 1. list item

View File

@ -34,6 +34,7 @@ typedef struct tBlockListItem
MD_BLOCK_CODE_DETAIL codeDetail; MD_BLOCK_CODE_DETAIL codeDetail;
} u; } u;
int numTabs; int numTabs;
tStyleType styleType;
struct tBlockListItem * next; struct tBlockListItem * next;
} tBlockListItem; } tBlockListItem;
@ -67,6 +68,8 @@ static int debugIndentLevel = 0;
static tBlockListItem * blockList = NULL; static tBlockListItem * blockList = NULL;
static uint16_t textStyleMask = STYLE_TEXT_PLAIN;
static tEntity entities[] = { static tEntity entities[] = {
{ "&Tab;", 0x9, 0x9 }, { "&Tab;", 0x9, 0x9 },
{ "&NewLine;", 0x13, 0x10 }, { "&NewLine;", 0x13, 0x10 },
@ -298,6 +301,8 @@ static tEntity entities[] = {
static int enterBlockHook(MD_BLOCKTYPE type, void * detail, void * userdata) static int enterBlockHook(MD_BLOCKTYPE type, void * detail, void * userdata)
{ {
static int isFirstNonDocumentBlock = 1; static int isFirstNonDocumentBlock = 1;
int shouldInsertCR = 1;
uint16_t headerSize = 0;
tBlockListItem * newBlock = malloc(sizeof(tBlockListItem)); tBlockListItem * newBlock = malloc(sizeof(tBlockListItem));
if (newBlock == NULL) { if (newBlock == NULL) {
@ -306,10 +311,15 @@ static int enterBlockHook(MD_BLOCKTYPE type, void * detail, void * userdata)
} }
newBlock->type = type; newBlock->type = type;
if (blockList == NULL) if (blockList == NULL) {
newBlock->numTabs = 0; newBlock->numTabs = 0;
else newBlock->styleType = STYLE_TYPE_TEXT;
} else {
newBlock->numTabs = blockList->numTabs; newBlock->numTabs = blockList->numTabs;
if (blockList->styleType == STYLE_TYPE_QUOTE)
newBlock->styleType = STYLE_TYPE_QUOTE;
newBlock->styleType = STYLE_TYPE_TEXT;
}
newBlock->next = blockList; newBlock->next = blockList;
blockList = newBlock; blockList = newBlock;
@ -321,12 +331,15 @@ static int enterBlockHook(MD_BLOCKTYPE type, void * detail, void * userdata)
case MD_BLOCK_DOC: case MD_BLOCK_DOC:
if (debugEnabled) if (debugEnabled)
fprintf(stderr, "%*sDOC {\n", debugIndentLevel, ""); fprintf(stderr, "%*sDOC {\n", debugIndentLevel, "");
newBlock->styleType = STYLE_TYPE_TEXT;
shouldInsertCR = 0;
break; break;
case MD_BLOCK_QUOTE: case MD_BLOCK_QUOTE:
if (debugEnabled) if (debugEnabled)
fprintf(stderr, "%*sQUOTE {\n", debugIndentLevel, ""); fprintf(stderr, "%*sQUOTE {\n", debugIndentLevel, "");
newBlock->styleType = STYLE_TYPE_QUOTE;
shouldInsertCR = 0;
break; break;
case MD_BLOCK_UL: { case MD_BLOCK_UL: {
@ -336,9 +349,6 @@ static int enterBlockHook(MD_BLOCKTYPE type, void * detail, void * userdata)
memcpy(&(newBlock->u.ulDetail), ulDetail, sizeof(*ulDetail)); memcpy(&(newBlock->u.ulDetail), ulDetail, sizeof(*ulDetail));
newBlock->numTabs++; newBlock->numTabs++;
if (!isFirstNonDocumentBlock)
writeChar('\r');
break; break;
} }
@ -349,17 +359,12 @@ static int enterBlockHook(MD_BLOCKTYPE type, void * detail, void * userdata)
memcpy(&(newBlock->u.olDetail), olDetail, sizeof(*olDetail)); memcpy(&(newBlock->u.olDetail), olDetail, sizeof(*olDetail));
newBlock->numTabs++; newBlock->numTabs++;
if (!isFirstNonDocumentBlock)
writeChar('\r');
break; break;
} }
case MD_BLOCK_LI: { case MD_BLOCK_LI: {
int i; int i;
tBlockListItem * enclosingBlock = newBlock->next; tBlockListItem * enclosingBlock = newBlock->next;
int isNumbered = 0;
static char str[16];
if (debugEnabled) if (debugEnabled)
fprintf(stderr, "%*sLI {\n", debugIndentLevel, ""); fprintf(stderr, "%*sLI {\n", debugIndentLevel, "");
@ -370,43 +375,18 @@ static int enterBlockHook(MD_BLOCKTYPE type, void * detail, void * userdata)
} }
if (enclosingBlock->type == MD_BLOCK_OL) { if (enclosingBlock->type == MD_BLOCK_OL) {
isNumbered = 1; shouldInsertCR = !enclosingBlock->u.olDetail.is_tight;
if ((!enclosingBlock->u.olDetail.is_tight) &&
(!isFirstNonDocumentBlock))
writeChar('\r');
} else if (enclosingBlock->type == MD_BLOCK_UL) { } else if (enclosingBlock->type == MD_BLOCK_UL) {
if ((!enclosingBlock->u.ulDetail.is_tight) && shouldInsertCR = !enclosingBlock->u.ulDetail.is_tight;
(!isFirstNonDocumentBlock))
writeChar('\r');
} }
for (i = 0; i < newBlock->numTabs; i++)
writeChar('\t');
if (isNumbered) {
sprintf(str, "%u%c ", enclosingBlock->u.olDetail.start, enclosingBlock->u.olDetail.mark_delimiter);
enclosingBlock->u.olDetail.start++;
} else {
sprintf(str, "%c ", 0xa5); // 0xa5 is a bullet character
}
writeString(str, strlen(str));
break; break;
} }
case MD_BLOCK_HR: { case MD_BLOCK_HR:
int i;
if (debugEnabled) if (debugEnabled)
fprintf(stderr, "%*sHR {\n", debugIndentLevel, ""); fprintf(stderr, "%*sHR {\n", debugIndentLevel, "");
if (!isFirstNonDocumentBlock)
writeChar('\r');
for (i = 0; i < 30; i++)
writeChar('_');
break; break;
}
case MD_BLOCK_H: { case MD_BLOCK_H: {
MD_BLOCK_H_DETAIL * hDetail = (MD_BLOCK_H_DETAIL *)detail; MD_BLOCK_H_DETAIL * hDetail = (MD_BLOCK_H_DETAIL *)detail;
@ -414,9 +394,12 @@ static int enterBlockHook(MD_BLOCKTYPE type, void * detail, void * userdata)
fprintf(stderr, "%*sH (level=%u) {\n", debugIndentLevel, "", hDetail->level); fprintf(stderr, "%*sH (level=%u) {\n", debugIndentLevel, "", hDetail->level);
memcpy(&(newBlock->u.hDetail), hDetail, sizeof(*hDetail)); memcpy(&(newBlock->u.hDetail), hDetail, sizeof(*hDetail));
setStyle(STYLE_TYPE_TEXT, textStyleMask, headerSize);
if (!isFirstNonDocumentBlock) if (!isFirstNonDocumentBlock)
writeChar('\r'); writeChar('\r');
headerSize = hDetail->level;
shouldInsertCR = 0;
newBlock->styleType = STYLE_TYPE_HEADER;
break; break;
} }
@ -431,18 +414,13 @@ static int enterBlockHook(MD_BLOCKTYPE type, void * detail, void * userdata)
} }
memcpy(&(newBlock->u.codeDetail), codeDetail, sizeof(*codeDetail)); memcpy(&(newBlock->u.codeDetail), codeDetail, sizeof(*codeDetail));
newBlock->styleType = STYLE_TYPE_CODE;
if (!isFirstNonDocumentBlock)
writeChar('\r');
break; break;
} }
case MD_BLOCK_P: case MD_BLOCK_P:
if (debugEnabled) if (debugEnabled)
fprintf(stderr, "%*sP {\n", debugIndentLevel, ""); fprintf(stderr, "%*sP {\n", debugIndentLevel, "");
if (!isFirstNonDocumentBlock)
writeChar('\r');
break; break;
default: default:
@ -451,10 +429,56 @@ static int enterBlockHook(MD_BLOCKTYPE type, void * detail, void * userdata)
break; break;
} }
setStyle(newBlock->styleType, textStyleMask, headerSize);
if ((!isFirstNonDocumentBlock) &&
(shouldInsertCR))
writeChar('\r');
switch (type) {
case MD_BLOCK_LI: {
int i;
tBlockListItem * enclosingBlock = newBlock->next;
static char str[16];
for (i = 0; i < newBlock->numTabs; i++)
writeChar('\t');
if (enclosingBlock->type == MD_BLOCK_OL) {
sprintf(str, "%u%c ", enclosingBlock->u.olDetail.start, enclosingBlock->u.olDetail.mark_delimiter);
enclosingBlock->u.olDetail.start++;
} else {
sprintf(str, "%c ", 0xa5); // 0xa5 is a bullet character
}
writeString(str, strlen(str));
break;
}
case MD_BLOCK_HR: {
int i;
for (i = 0; i < 30; i++)
writeChar('_');
break;
}
case MD_BLOCK_DOC:
case MD_BLOCK_QUOTE:
case MD_BLOCK_UL:
case MD_BLOCK_OL:
case MD_BLOCK_H:
case MD_BLOCK_CODE:
case MD_BLOCK_P:
break;
default:
fprintf(stderr, "%s: Invalid block type (%d)\n", commandName, (int)type);
return 1;
break;
}
if (type != MD_BLOCK_DOC) if (type != MD_BLOCK_DOC)
isFirstNonDocumentBlock = 0; isFirstNonDocumentBlock = 0;
debugIndentLevel+=2; debugIndentLevel+=2;
return 0; return 0;
} }
@ -522,6 +546,9 @@ static int leaveBlockHook(MD_BLOCKTYPE type, void * detail, void * userdata)
break; break;
} }
if (blockList != NULL)
setStyle(blockList->styleType, textStyleMask, 0);
debugIndentLevel-=2; debugIndentLevel-=2;
if (debugEnabled) if (debugEnabled)
fprintf(stderr, "%*s}\n", debugIndentLevel, ""); fprintf(stderr, "%*s}\n", debugIndentLevel, "");
@ -540,11 +567,17 @@ static int enterSpanHook(MD_SPANTYPE type, void * detail, void * userdata)
case MD_SPAN_EM: case MD_SPAN_EM:
if (debugEnabled) if (debugEnabled)
fprintf(stderr, "%*sEM {\n", debugIndentLevel, ""); fprintf(stderr, "%*sEM {\n", debugIndentLevel, "");
textStyleMask |= STYLE_TEXT_MASK_EMPHASIZED;
setStyle(blockList->styleType, textStyleMask, blockList->u.hDetail.level);
break; break;
case MD_SPAN_STRONG: case MD_SPAN_STRONG:
if (debugEnabled) if (debugEnabled)
fprintf(stderr, "%*sSTRONG {\n", debugIndentLevel, ""); fprintf(stderr, "%*sSTRONG {\n", debugIndentLevel, "");
textStyleMask |= STYLE_TEXT_MASK_STRONG;
setStyle(blockList->styleType, textStyleMask, blockList->u.hDetail.level);
break; break;
case MD_SPAN_A: case MD_SPAN_A:
@ -560,6 +593,7 @@ static int enterSpanHook(MD_SPANTYPE type, void * detail, void * userdata)
case MD_SPAN_CODE: case MD_SPAN_CODE:
if (debugEnabled) if (debugEnabled)
fprintf(stderr, "%*sCODE {\n", debugIndentLevel, ""); fprintf(stderr, "%*sCODE {\n", debugIndentLevel, "");
setStyle(STYLE_TYPE_CODE, STYLE_TEXT_PLAIN, 0);
break; break;
default: default:
@ -581,9 +615,13 @@ static int leaveSpanHook(MD_SPANTYPE type, void * detail, void * userdata)
switch (type) { switch (type) {
case MD_SPAN_EM: case MD_SPAN_EM:
textStyleMask &= ~STYLE_TEXT_MASK_EMPHASIZED;
setStyle(blockList->styleType, textStyleMask, blockList->u.hDetail.level);
break; break;
case MD_SPAN_STRONG: case MD_SPAN_STRONG:
textStyleMask &= ~STYLE_TEXT_MASK_STRONG;
setStyle(blockList->styleType, textStyleMask, blockList->u.hDetail.level);
break; break;
case MD_SPAN_A: case MD_SPAN_A:
@ -593,6 +631,7 @@ static int leaveSpanHook(MD_SPANTYPE type, void * detail, void * userdata)
break; break;
case MD_SPAN_CODE: case MD_SPAN_CODE:
setStyle(blockList->styleType, textStyleMask, blockList->u.hDetail.level);
break; break;
default: default:
@ -670,7 +709,7 @@ static int textHook(MD_TEXTTYPE type, const MD_CHAR * text, MD_SIZE size, void *
case MD_TEXT_BR: case MD_TEXT_BR:
if (debugEnabled) if (debugEnabled)
fprintf(stderr, "%*sBR\n", debugIndentLevel, ""); fprintf(stderr, "%*sBR\n", debugIndentLevel, "");
putchar('\n'); writeChar('\n');
return 0; return 0;
case MD_TEXT_SOFTBR: case MD_TEXT_SOFTBR: