Add htmlstriptags function.

This commit is contained in:
Dianne Skoll
2023-03-02 11:40:03 -05:00
parent 87e392de6c
commit d4a183f3bf
3 changed files with 56 additions and 1 deletions

View File

@@ -3078,6 +3078,12 @@ Returns the hour component of \fItime\fR.
Returns a modified copy of \fIstr\fR where "<" is replaced with
"&lt;"; ">" is replaced with "&gt;" and "&" is replaced with "&amp;"
.TP
.B htmlstriptags(s_str)
Returns a modified copy of \fIstr\fR where HTML tags are stripped
out. The stripping algorithm is fairly naive; the function starts
stripping characters when it encounters a "<" and it stops stripping
when it encounters a ">".
.TP
.B iif(si_test1, x_arg1, [si_test2, x_arg2,...], x_default)
If \fItest1\fR is not zero or the null string, returns \fIarg1\fR.
Otherwise, if \fItest2\fR is not zero or the null string, returns

View File

@@ -102,6 +102,7 @@ static int FHebmon (func_info *);
static int FHebyear (func_info *);
static int FHour (func_info *);
static int FHtmlEscape (func_info *);
static int FHtmlStriptags (func_info *);
static int FIif (func_info *);
static int FIndex (func_info *);
static int FIsdst (func_info *);
@@ -267,6 +268,7 @@ BuiltinFunc Func[] = {
{ "hebyear", 1, 1, 0, FHebyear },
{ "hour", 1, 1, 1, FHour },
{ "htmlescape", 1, 1, 1, FHtmlEscape },
{ "htmlstriptags",1, 1, 1, FHtmlStriptags },
{ "iif", 1, NO_MAX, 1, FIif },
{ "index", 2, 3, 1, FIndex },
{ "isany", 1, NO_MAX, 1, FIsAny },
@@ -2268,7 +2270,6 @@ static int FHebyear(func_info *info)
/* htmlescape - replace <. > and & by &lt; &gt; and &amp; */
/* */
/****************************************************************/
static int FHtmlEscape(func_info *info)
{
DynamicBuffer dbuf;
@@ -2309,6 +2310,46 @@ static int FHtmlEscape(func_info *info)
return r;
}
/****************************************************************/
/* */
/* htmlstriptags - strip out HTML tags from a string */
/* */
/****************************************************************/
static int FHtmlStriptags(func_info *info)
{
DynamicBuffer dbuf;
char const *s;
int r = OK;
int in_tag = 0;
ASSERT_TYPE(0, STR_TYPE);
DBufInit(&dbuf);
s = ARGSTR(0);
while(*s) {
if (!in_tag) {
if (*s == '<') {
in_tag = 1;
} else {
r = DBufPutc(&dbuf, *s);
}
} else {
if (*s == '>') {
in_tag = 0;
}
}
if (r != OK) {
DBufFree(&dbuf);
return r;
}
s++;
}
r = RetStrVal(DBufValue(&dbuf), info);
DBufFree(&dbuf);
return r;
}
/****************************************************************/
/* */
/* FEasterdate - calc. easter Sunday from a year. */

View File

@@ -873,6 +873,14 @@ set a htmlescape("<&>")
REM MSG [a]
set a htmlescape("@&^#*@&^##$*&@><><@#@#><@#>%%_#$foobarquux")
REM MSG [a]
# htmlstriptags
set a htmlstriptags("foobar")
set a htmlstriptags("This is <b>bold</b>")
set a htmlstriptags("This is <unclosed whut?")
set a htmlstriptags("this is > whut <b>foo</b>")
set a htmlstriptags("<img src=\"foo\">")
# Don't want Remind to queue reminders
EXIT