diff options
author | Jaakko Keränen <jaakko.keranen@iki.fi> | 2022-02-04 06:25:52 +0200 |
---|---|---|
committer | Jaakko Keränen <jaakko.keranen@iki.fi> | 2022-02-04 06:25:52 +0200 |
commit | 99d6297a58c08926d4316b80c720ac602e236e6c (patch) | |
tree | 02bc5c922ecff4786caae135e93e88dc81fd3cff /src/gmdocument.c | |
parent | a7163a05daad0ae4b04624eb954d361a75cd33cb (diff) |
Fixed issues with stripping ANSI escapes
Diffstat (limited to 'src/gmdocument.c')
-rw-r--r-- | src/gmdocument.c | 72 |
1 files changed, 7 insertions, 65 deletions
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, | |||
613 | } | 613 | } |
614 | 614 | ||
615 | static void doLayout_GmDocument_(iGmDocument *d) { | 615 | static void doLayout_GmDocument_(iGmDocument *d) { |
616 | static iRegExp *ansiPattern_; | ||
617 | if (!ansiPattern_) { | ||
618 | ansiPattern_ = makeAnsiEscapePattern_Text(iTrue /* with ESC */); | ||
619 | } | ||
616 | const iPrefs *prefs = prefs_App(); | 620 | const iPrefs *prefs = prefs_App(); |
617 | const iBool isMono = isForcedMonospace_GmDocument_(d); | 621 | const iBool isMono = isForcedMonospace_GmDocument_(d); |
618 | const iBool isGopher = isGopher_GmDocument_(d); | 622 | const iBool isGopher = isGopher_GmDocument_(d); |
@@ -620,8 +624,7 @@ static void doLayout_GmDocument_(iGmDocument *d) { | |||
620 | const iBool isVeryNarrow = d->size.x <= 70 * gap_Text; | 624 | const iBool isVeryNarrow = d->size.x <= 70 * gap_Text; |
621 | const iBool isExtremelyNarrow = d->size.x <= 60 * gap_Text; | 625 | const iBool isExtremelyNarrow = d->size.x <= 60 * gap_Text; |
622 | const iBool isFullWidthImages = (d->outsideMargin < 5 * gap_UI); | 626 | const iBool isFullWidthImages = (d->outsideMargin < 5 * gap_UI); |
623 | // const iBool isDarkBg = isDark_GmDocumentTheme( | 627 | |
624 | // isDark_ColorTheme(colorTheme_App()) ? prefs->docThemeDark : prefs->docThemeLight); | ||
625 | initTheme_GmDocument_(d); | 628 | initTheme_GmDocument_(d); |
626 | d->isLayoutInvalidated = iFalse; | 629 | d->isLayoutInvalidated = iFalse; |
627 | /* TODO: Collect these parameters into a GmTheme. */ | 630 | /* TODO: Collect these parameters into a GmTheme. */ |
@@ -659,7 +662,6 @@ static void doLayout_GmDocument_(iGmDocument *d) { | |||
659 | const iArray *oldPreMeta = collect_Array(copy_Array(&d->preMeta)); /* remember fold states */ | 662 | const iArray *oldPreMeta = collect_Array(copy_Array(&d->preMeta)); /* remember fold states */ |
660 | clear_Array(&d->preMeta); | 663 | clear_Array(&d->preMeta); |
661 | clear_String(&d->title); | 664 | clear_String(&d->title); |
662 | // clear_String(&d->bannerText); | ||
663 | if (d->size.x <= 0 || isEmpty_String(&d->source)) { | 665 | if (d->size.x <= 0 || isEmpty_String(&d->source)) { |
664 | return; | 666 | return; |
665 | } | 667 | } |
@@ -673,7 +675,6 @@ static void doLayout_GmDocument_(iGmDocument *d) { | |||
673 | int preFont = preformatted_FontId; | 675 | int preFont = preformatted_FontId; |
674 | uint16_t preId = 0; | 676 | uint16_t preId = 0; |
675 | iBool enableIndents = iFalse; | 677 | iBool enableIndents = iFalse; |
676 | // iBool addSiteBanner = d->bannerType != none_GmDocumentBanner; | ||
677 | const iBool isNormalized = isNormalized_GmDocument_(d); | 678 | const iBool isNormalized = isNormalized_GmDocument_(d); |
678 | enum iGmLineType prevType = text_GmLineType; | 679 | enum iGmLineType prevType = text_GmLineType; |
679 | enum iGmLineType prevNonBlankType = text_GmLineType; | 680 | enum iGmLineType prevNonBlankType = text_GmLineType; |
@@ -757,7 +758,6 @@ static void doLayout_GmDocument_(iGmDocument *d) { | |||
757 | if (d->format == gemini_SourceFormat && | 758 | if (d->format == gemini_SourceFormat && |
758 | startsWithSc_Rangecc(line, "```", &iCaseSensitive)) { | 759 | startsWithSc_Rangecc(line, "```", &iCaseSensitive)) { |
759 | isPreformat = iFalse; | 760 | isPreformat = iFalse; |
760 | // addSiteBanner = iFalse; /* overrides the banner */ | ||
761 | continue; | 761 | continue; |
762 | } | 762 | } |
763 | run.mediaType = max_MediaType; /* preformatted block */ | 763 | run.mediaType = max_MediaType; /* preformatted block */ |
@@ -765,28 +765,6 @@ static void doLayout_GmDocument_(iGmDocument *d) { | |||
765 | run.font = (d->format == plainText_SourceFormat ? plainText_FontId : preFont); | 765 | run.font = (d->format == plainText_SourceFormat ? plainText_FontId : preFont); |
766 | indent = indents[type]; | 766 | indent = indents[type]; |
767 | } | 767 | } |
768 | #if 0 | ||
769 | if (addSiteBanner) { | ||
770 | addSiteBanner = iFalse; | ||
771 | const iRangecc bannerText = urlHost_String(&d->url); | ||
772 | if (!isEmpty_Range(&bannerText)) { | ||
773 | setRange_String(&d->bannerText, bannerText); | ||
774 | iGmRun banner = { .flags = decoration_GmRunFlag | siteBanner_GmRunFlag }; | ||
775 | banner.bounds = zero_Rect(); | ||
776 | banner.visBounds = init_Rect(0, 0, d->size.x, lineHeight_Text(banner_FontId) * 2); | ||
777 | if (d->bannerType == certificateWarning_GmDocumentBanner) { | ||
778 | banner.visBounds.size.y += iMaxi(6000 * lineHeight_Text(uiLabel_FontId) / | ||
779 | d->size.x, lineHeight_Text(uiLabel_FontId) * 5); | ||
780 | } | ||
781 | banner.text = bannerText; | ||
782 | banner.font = banner_FontId; | ||
783 | banner.color = tmBannerTitle_ColorId; | ||
784 | pushBack_Array(&d->layout, &banner); | ||
785 | pos.y += height_Rect(banner.visBounds) + | ||
786 | 1.5f * lineHeight_Text(paragraph_FontId) * prefs->lineSpacing; | ||
787 | } | ||
788 | } | ||
789 | #endif | ||
790 | /* Empty lines don't produce text runs. */ | 768 | /* Empty lines don't produce text runs. */ |
791 | if (isEmpty_Range(&line)) { | 769 | if (isEmpty_Range(&line)) { |
792 | if (type == quote_GmLineType && !prefs->quoteIcon) { | 770 | if (type == quote_GmLineType && !prefs->quoteIcon) { |
@@ -867,6 +845,8 @@ static void doLayout_GmDocument_(iGmDocument *d) { | |||
867 | if ((type == heading1_GmLineType || type == heading2_GmLineType) && | 845 | if ((type == heading1_GmLineType || type == heading2_GmLineType) && |
868 | isEmpty_String(&d->title)) { | 846 | isEmpty_String(&d->title)) { |
869 | setRange_String(&d->title, line); | 847 | setRange_String(&d->title, line); |
848 | /* Get rid of ANSI escapes. */ | ||
849 | replaceRegExp_String(&d->title, ansiPattern_, "", NULL, NULL); | ||
870 | } | 850 | } |
871 | /* List bullet. */ | 851 | /* List bullet. */ |
872 | if (type == bullet_GmLineType) { | 852 | if (type == bullet_GmLineType) { |
@@ -1959,44 +1939,6 @@ void setUrl_GmDocument(iGmDocument *d, const iString *url) { | |||
1959 | } | 1939 | } |
1960 | } | 1940 | } |
1961 | 1941 | ||
1962 | int replaceRegExp_String(iString *d, const iRegExp *regexp, const char *replacement, | ||
1963 | void (*matchHandler)(void *, const iRegExpMatch *), | ||
1964 | void *context) { | ||
1965 | iRegExpMatch m; | ||
1966 | iString result; | ||
1967 | int numMatches = 0; | ||
1968 | const char *pos = constBegin_String(d); | ||
1969 | init_RegExpMatch(&m); | ||
1970 | init_String(&result); | ||
1971 | while (matchString_RegExp(regexp, d, &m)) { | ||
1972 | appendRange_String(&result, (iRangecc){ pos, begin_RegExpMatch(&m) }); | ||
1973 | /* Replace any capture group back-references. */ | ||
1974 | for (const char *ch = replacement; *ch; ch++) { | ||
1975 | if (*ch == '\\') { | ||
1976 | ch++; | ||
1977 | if (*ch == '\\') { | ||
1978 | appendCStr_String(&result, "\\"); | ||
1979 | } | ||
1980 | else if (*ch >= '0' && *ch <= '9') { | ||
1981 | appendRange_String(&result, capturedRange_RegExpMatch(&m, *ch - '0')); | ||
1982 | } | ||
1983 | } | ||
1984 | else { | ||
1985 | appendData_Block(&result.chars, ch, 1); | ||
1986 | } | ||
1987 | } | ||
1988 | if (matchHandler) { | ||
1989 | matchHandler(context, &m); | ||
1990 | } | ||
1991 | pos = end_RegExpMatch(&m); | ||
1992 | numMatches++; | ||
1993 | } | ||
1994 | appendRange_String(&result, (iRangecc){ pos, constEnd_String(d) }); | ||
1995 | set_String(d, &result); | ||
1996 | deinit_String(&result); | ||
1997 | return numMatches; | ||
1998 | } | ||
1999 | |||
2000 | iDeclareType(PendingLink) | 1942 | iDeclareType(PendingLink) |
2001 | struct Impl_PendingLink { | 1943 | struct Impl_PendingLink { |
2002 | iString *url; | 1944 | iString *url; |