summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--filter.c17
-rw-r--r--txr.120
2 files changed, 36 insertions, 1 deletions
diff --git a/filter.c b/filter.c
index d7812454..70e6cf87 100644
--- a/filter.c
+++ b/filter.c
@@ -45,6 +45,7 @@
val filters;
val filter_k, lfilt_k, rfilt_k, to_html_k, from_html_k;
+val to_html_relaxed_k;
val upcase_k, downcase_k;
val topercent_k, frompercent_k, tourl_k, fromurl_k;
val tonumber_k, toint_k, tofloat_k, hextoint_k;
@@ -284,6 +285,13 @@ static struct filter_pair to_html_table[] = {
{ 0, 0 }
};
+static struct filter_pair to_html_relaxed_table[] = {
+ { wli("<"), wli("&lt;") },
+ { wli(">"), wli("&gt;") },
+ { wli("&"), wli("&amp;") },
+ { 0, 0 }
+};
+
static struct filter_pair from_html_table[] = {
{ wli("&quot;"), wli("\"") },
{ wli("&amp;"), wli("&") },
@@ -675,6 +683,11 @@ static val html_encode(val str)
return trie_filter_string(get_filter(to_html_k), str);
}
+static val html_encode_star(val str)
+{
+ return trie_filter_string(get_filter(to_html_relaxed_k), str);
+}
+
static val html_decode(val str)
{
return trie_filter_string(get_filter(from_html_k), str);
@@ -689,6 +702,7 @@ void filter_init(void)
lfilt_k = intern(lit("lfilt"), keyword_package);
rfilt_k = intern(lit("rfilt"), keyword_package);
to_html_k = intern(lit("to_html"), keyword_package);
+ to_html_relaxed_k = intern(lit("to_html_relaxed"), keyword_package);
from_html_k = intern(lit("from_html"), keyword_package);
upcase_k = intern(lit("upcase"), keyword_package);
downcase_k = intern(lit("downcase"), keyword_package);
@@ -702,6 +716,7 @@ void filter_init(void)
hextoint_k = intern(lit("hextoint"), keyword_package);
sethash(filters, to_html_k, build_filter(to_html_table, t));
+ sethash(filters, to_html_relaxed_k, build_filter(to_html_relaxed_table, t));
{
val trie = build_filter(from_html_table, nil);
trie_add(trie, lit("&#"), func_n1(html_numeric_handler));
@@ -731,5 +746,7 @@ void filter_init(void)
reg_fun(intern(lit("url-encode"), user_package), func_n2o(url_encode, 1));
reg_fun(intern(lit("url-decode"), user_package), func_n2o(url_decode, 1));
reg_fun(intern(lit("html-encode"), user_package), func_n1(html_encode));
+ reg_fun(intern(lit("html-encode*"), user_package),
+ func_n1(html_encode_star));
reg_fun(intern(lit("html-decode"), user_package), func_n1(html_decode));
}
diff --git a/txr.1 b/txr.1
index 45d49946..62e48c8f 100644
--- a/txr.1
+++ b/txr.1
@@ -7629,6 +7629,14 @@ ampersand sequences. For instance
is replaced by
.codn &gt; .
+.coIP :to_html_relaxed
+Filter text to HTML, representing special characters using HTML
+ampersand sequences. Unlike
+.codn :to_html ,
+this filter doesn't treat the single and double quote characters.
+It is not suitable for preparing HTML fragments which end up
+inserted into HTML tag attributes.
+
.coIP :from_html
Filter text with HTML codes into text in which the codes are replaced by the
corresponding characters. For instance
@@ -35622,7 +35630,7 @@ encoded data are retained as
characters in the decoded strings. Otherwise,
plus characters are converted to spaces.
-.coNP Functions @ html-encode and @ html-decode
+.coNP Functions @, html-encode @, html-encode* and @ html-decode
.synb
.mets (html-encode << text-string )
.mets (html-decode << html-string )
@@ -35673,6 +35681,16 @@ From this,
produces
.strn "&lt;p&gt;Hello, world!&lt;/p&gt;" .
+The
+.code html-encode*
+function is similar to
+.code html-encode
+except that it does not encode the single and double quote characters
+(ASCII 39 and 34, respectively). Text prepared by this function may not
+be suitable for insertion into a HTML template, depending on the
+context of its insertion. It is suitable as text placed between
+tags but not necessary as tag attribute material.
+
.SS* Filter Module
The filter module provides a trie (pronounced "try") data structure,
which is suitable for representing dictionaries for efficient filtering.