From 99d6297a58c08926d4316b80c720ac602e236e6c Mon Sep 17 00:00:00 2001 From: Jaakko Keränen Date: Fri, 4 Feb 2022 06:25:52 +0200 Subject: Fixed issues with stripping ANSI escapes --- src/gmdocument.c | 72 ++++++-------------------------------------------------- 1 file changed, 7 insertions(+), 65 deletions(-) (limited to 'src/gmdocument.c') diff --git a/src/gmdocument.c b/src/gmdocument.c index 5bb1dd21..b5e71e21 100644 --- a/src/gmdocument.c +++ b/src/gmdocument.c @@ -613,6 +613,10 @@ static iBool typesetOneLine_RunTypesetter_(iWrapText *wrap, iRangecc wrapRange, } static void doLayout_GmDocument_(iGmDocument *d) { + static iRegExp *ansiPattern_; + if (!ansiPattern_) { + ansiPattern_ = makeAnsiEscapePattern_Text(iTrue /* with ESC */); + } const iPrefs *prefs = prefs_App(); const iBool isMono = isForcedMonospace_GmDocument_(d); const iBool isGopher = isGopher_GmDocument_(d); @@ -620,8 +624,7 @@ static void doLayout_GmDocument_(iGmDocument *d) { const iBool isVeryNarrow = d->size.x <= 70 * gap_Text; const iBool isExtremelyNarrow = d->size.x <= 60 * gap_Text; const iBool isFullWidthImages = (d->outsideMargin < 5 * gap_UI); -// const iBool isDarkBg = isDark_GmDocumentTheme( -// isDark_ColorTheme(colorTheme_App()) ? prefs->docThemeDark : prefs->docThemeLight); + initTheme_GmDocument_(d); d->isLayoutInvalidated = iFalse; /* TODO: Collect these parameters into a GmTheme. */ @@ -659,7 +662,6 @@ static void doLayout_GmDocument_(iGmDocument *d) { const iArray *oldPreMeta = collect_Array(copy_Array(&d->preMeta)); /* remember fold states */ clear_Array(&d->preMeta); clear_String(&d->title); -// clear_String(&d->bannerText); if (d->size.x <= 0 || isEmpty_String(&d->source)) { return; } @@ -673,7 +675,6 @@ static void doLayout_GmDocument_(iGmDocument *d) { int preFont = preformatted_FontId; uint16_t preId = 0; iBool enableIndents = iFalse; -// iBool addSiteBanner = d->bannerType != none_GmDocumentBanner; const iBool isNormalized = isNormalized_GmDocument_(d); enum iGmLineType prevType = text_GmLineType; enum iGmLineType prevNonBlankType = text_GmLineType; @@ -757,7 +758,6 @@ static void doLayout_GmDocument_(iGmDocument *d) { if (d->format == gemini_SourceFormat && startsWithSc_Rangecc(line, "```", &iCaseSensitive)) { isPreformat = iFalse; -// addSiteBanner = iFalse; /* overrides the banner */ continue; } run.mediaType = max_MediaType; /* preformatted block */ @@ -765,28 +765,6 @@ static void doLayout_GmDocument_(iGmDocument *d) { run.font = (d->format == plainText_SourceFormat ? plainText_FontId : preFont); indent = indents[type]; } -#if 0 - if (addSiteBanner) { - addSiteBanner = iFalse; - const iRangecc bannerText = urlHost_String(&d->url); - if (!isEmpty_Range(&bannerText)) { - setRange_String(&d->bannerText, bannerText); - iGmRun banner = { .flags = decoration_GmRunFlag | siteBanner_GmRunFlag }; - banner.bounds = zero_Rect(); - banner.visBounds = init_Rect(0, 0, d->size.x, lineHeight_Text(banner_FontId) * 2); - if (d->bannerType == certificateWarning_GmDocumentBanner) { - banner.visBounds.size.y += iMaxi(6000 * lineHeight_Text(uiLabel_FontId) / - d->size.x, lineHeight_Text(uiLabel_FontId) * 5); - } - banner.text = bannerText; - banner.font = banner_FontId; - banner.color = tmBannerTitle_ColorId; - pushBack_Array(&d->layout, &banner); - pos.y += height_Rect(banner.visBounds) + - 1.5f * lineHeight_Text(paragraph_FontId) * prefs->lineSpacing; - } - } -#endif /* Empty lines don't produce text runs. */ if (isEmpty_Range(&line)) { if (type == quote_GmLineType && !prefs->quoteIcon) { @@ -867,6 +845,8 @@ static void doLayout_GmDocument_(iGmDocument *d) { if ((type == heading1_GmLineType || type == heading2_GmLineType) && isEmpty_String(&d->title)) { setRange_String(&d->title, line); + /* Get rid of ANSI escapes. */ + replaceRegExp_String(&d->title, ansiPattern_, "", NULL, NULL); } /* List bullet. */ if (type == bullet_GmLineType) { @@ -1959,44 +1939,6 @@ void setUrl_GmDocument(iGmDocument *d, const iString *url) { } } -int replaceRegExp_String(iString *d, const iRegExp *regexp, const char *replacement, - void (*matchHandler)(void *, const iRegExpMatch *), - void *context) { - iRegExpMatch m; - iString result; - int numMatches = 0; - const char *pos = constBegin_String(d); - init_RegExpMatch(&m); - init_String(&result); - while (matchString_RegExp(regexp, d, &m)) { - appendRange_String(&result, (iRangecc){ pos, begin_RegExpMatch(&m) }); - /* Replace any capture group back-references. */ - for (const char *ch = replacement; *ch; ch++) { - if (*ch == '\\') { - ch++; - if (*ch == '\\') { - appendCStr_String(&result, "\\"); - } - else if (*ch >= '0' && *ch <= '9') { - appendRange_String(&result, capturedRange_RegExpMatch(&m, *ch - '0')); - } - } - else { - appendData_Block(&result.chars, ch, 1); - } - } - if (matchHandler) { - matchHandler(context, &m); - } - pos = end_RegExpMatch(&m); - numMatches++; - } - appendRange_String(&result, (iRangecc){ pos, constEnd_String(d) }); - set_String(d, &result); - deinit_String(&result); - return numMatches; -} - iDeclareType(PendingLink) struct Impl_PendingLink { iString *url; -- cgit v1.2.3