summaryrefslogtreecommitdiff
path: root/src/gmdocument.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/gmdocument.c')
-rw-r--r--src/gmdocument.c32
1 files changed, 26 insertions, 6 deletions
diff --git a/src/gmdocument.c b/src/gmdocument.c
index 5389aac7..1a8312f0 100644
--- a/src/gmdocument.c
+++ b/src/gmdocument.c
@@ -27,6 +27,7 @@ iDefineTypeConstruction(GmLink)
27 27
28struct Impl_GmDocument { 28struct Impl_GmDocument {
29 iObject object; 29 iObject object;
30 enum iGmDocumentFormat format;
30 iString source; 31 iString source;
31 iString localHost; 32 iString localHost;
32 iInt2 size; 33 iInt2 size;
@@ -49,7 +50,10 @@ enum iGmLineType {
49 max_GmLineType, 50 max_GmLineType,
50}; 51};
51 52
52static enum iGmLineType lineType_Rangecc_(const iRangecc *line) { 53static enum iGmLineType lineType_GmDocument_(const iGmDocument *d, const iRangecc *line) {
54 if (d->format == plainText_GmDocumentFormat) {
55 return text_GmLineType;
56 }
53 if (isEmpty_Range(line)) { 57 if (isEmpty_Range(line)) {
54 return text_GmLineType; 58 return text_GmLineType;
55 } 59 }
@@ -207,6 +211,10 @@ static void doLayout_GmDocument_(iGmDocument *d) {
207 iBool isPreformat = iFalse; 211 iBool isPreformat = iFalse;
208 iBool isFirstText = iTrue; 212 iBool isFirstText = iTrue;
209 int preFont = preformatted_FontId; 213 int preFont = preformatted_FontId;
214 if (d->format == plainText_GmDocumentFormat) {
215 isPreformat = iTrue;
216 isFirstText = iFalse;
217 }
210 while (nextSplit_Rangecc(&content, "\n", &line)) { 218 while (nextSplit_Rangecc(&content, "\n", &line)) {
211 iGmRun run; 219 iGmRun run;
212 run.color = white_ColorId; 220 run.color = white_ColorId;
@@ -214,7 +222,7 @@ static void doLayout_GmDocument_(iGmDocument *d) {
214 enum iGmLineType type; 222 enum iGmLineType type;
215 int indent = 0; 223 int indent = 0;
216 if (!isPreformat) { 224 if (!isPreformat) {
217 type = lineType_Rangecc_(&line); 225 type = lineType_GmDocument_(d, &line);
218 indent = indents[type]; 226 indent = indents[type];
219 if (type == preformatted_GmLineType) { 227 if (type == preformatted_GmLineType) {
220 isPreformat = iTrue; 228 isPreformat = iTrue;
@@ -242,7 +250,8 @@ static void doLayout_GmDocument_(iGmDocument *d) {
242 else { 250 else {
243 /* Preformatted line. */ 251 /* Preformatted line. */
244 type = preformatted_GmLineType; 252 type = preformatted_GmLineType;
245 if (startsWithSc_Rangecc(&line, "```", &iCaseSensitive)) { 253 if (d->format == gemini_GmDocumentFormat &&
254 startsWithSc_Rangecc(&line, "```", &iCaseSensitive)) {
246 isPreformat = iFalse; 255 isPreformat = iFalse;
247 preAltText = iNullRange; 256 preAltText = iNullRange;
248 continue; 257 continue;
@@ -254,6 +263,7 @@ static void doLayout_GmDocument_(iGmDocument *d) {
254 if (isEmpty_Range(&line)) { 263 if (isEmpty_Range(&line)) {
255 pos.y += lineHeight_Text(run.font); 264 pos.y += lineHeight_Text(run.font);
256 prevType = text_GmLineType; 265 prevType = text_GmLineType;
266 /* TODO: Extra skip needed here? */
257 continue; 267 continue;
258 } 268 }
259 /* Check the margin vs. previous run. */ 269 /* Check the margin vs. previous run. */
@@ -304,6 +314,9 @@ static void doLayout_GmDocument_(iGmDocument *d) {
304 pushBack_Array(&d->layout, &run); 314 pushBack_Array(&d->layout, &run);
305 } 315 }
306 run.color = colors[type]; 316 run.color = colors[type];
317 if (d->format == plainText_GmDocumentFormat) {
318 run.color = colors[text_GmLineType];
319 }
307 /* Special formatting for the first paragraph (e.g., subtitle, introduction, or lede). */ 320 /* Special formatting for the first paragraph (e.g., subtitle, introduction, or lede). */
308 if (type == text_GmLineType && isFirstText) { 321 if (type == text_GmLineType && isFirstText) {
309 run.font = firstParagraph_FontId; 322 run.font = firstParagraph_FontId;
@@ -349,6 +362,7 @@ static void doLayout_GmDocument_(iGmDocument *d) {
349} 362}
350 363
351void init_GmDocument(iGmDocument *d) { 364void init_GmDocument(iGmDocument *d) {
365 d->format = gemini_GmDocumentFormat;
352 init_String(&d->source); 366 init_String(&d->source);
353 init_String(&d->localHost); 367 init_String(&d->localHost);
354 d->size = zero_I2(); 368 d->size = zero_I2();
@@ -366,6 +380,10 @@ void deinit_GmDocument(iGmDocument *d) {
366 deinit_String(&d->source); 380 deinit_String(&d->source);
367} 381}
368 382
383void setFormat_GmDocument(iGmDocument *d, enum iGmDocumentFormat format) {
384 d->format = format;
385}
386
369void setWidth_GmDocument(iGmDocument *d, int width) { 387void setWidth_GmDocument(iGmDocument *d, int width) {
370 d->size.x = width; 388 d->size.x = width;
371 doLayout_GmDocument_(d); /* TODO: just flag need-layout and do it later */ 389 doLayout_GmDocument_(d); /* TODO: just flag need-layout and do it later */
@@ -380,6 +398,9 @@ static void normalize_GmDocument(iGmDocument *d) {
380 iRangecc src = range_String(&d->source); 398 iRangecc src = range_String(&d->source);
381 iRangecc line = iNullRange; 399 iRangecc line = iNullRange;
382 iBool isPreformat = iFalse; 400 iBool isPreformat = iFalse;
401 if (d->format == plainText_GmDocumentFormat) {
402 isPreformat = iTrue; /* Cannot be turned off. */
403 }
383 const int preTabWidth = 8; /* TODO: user-configurable parameter */ 404 const int preTabWidth = 8; /* TODO: user-configurable parameter */
384 while (nextSplit_Rangecc(&src, "\n", &line)) { 405 while (nextSplit_Rangecc(&src, "\n", &line)) {
385 if (isPreformat) { 406 if (isPreformat) {
@@ -397,12 +418,12 @@ static void normalize_GmDocument(iGmDocument *d) {
397 } 418 }
398 } 419 }
399 appendCStr_String(normalized, "\n"); 420 appendCStr_String(normalized, "\n");
400 if (lineType_Rangecc_(&line) == preformatted_GmLineType) { 421 if (lineType_GmDocument_(d, &line) == preformatted_GmLineType) {
401 isPreformat = iFalse; 422 isPreformat = iFalse;
402 } 423 }
403 continue; 424 continue;
404 } 425 }
405 if (lineType_Rangecc_(&line) == preformatted_GmLineType) { 426 if (lineType_GmDocument_(d, &line) == preformatted_GmLineType) {
406 isPreformat = iTrue; 427 isPreformat = iTrue;
407 appendRange_String(normalized, line); 428 appendRange_String(normalized, line);
408 appendCStr_String(normalized, "\n"); 429 appendCStr_String(normalized, "\n");
@@ -426,7 +447,6 @@ static void normalize_GmDocument(iGmDocument *d) {
426 appendCStr_String(normalized, "\n"); 447 appendCStr_String(normalized, "\n");
427 } 448 }
428 set_String(&d->source, collect_String(normalized)); 449 set_String(&d->source, collect_String(normalized));
429// printf("normalized:\n%s\n", cstr_String(&d->source));
430} 450}
431 451
432void setHost_GmDocument(iGmDocument *d, const iString *host) { 452void setHost_GmDocument(iGmDocument *d, const iString *host) {