Replacing HTML codes

Q&A's, tips, howto's
Locked
kanen
Posts: 145
Joined: Thu Mar 25, 2010 6:24 pm
Contact:

Replacing HTML codes

Post by kanen »

I have a series of lists, which contain something like:

"Telefónica to Sell Spanish Assets"

I want to be able to quickly change all these codes to their proper ascii characters.

Code: Select all

(set 'x "Telefónica to Sell Spanish Assets")
(find {&#(.*);} x 0)  ; reference only
(regex {&#(.*);} x)
; > ("ó" 5 6 "243" 7 3)
> $1
; > "243"
(char (int $1))
; > "ó"
My question?

How can I do the above in one quick pass, where I get all the {&#(.*);} and replace them with (char (int $1)) ... without doing a loop through everything over and over until the (regex) finds nothing?

I feel like I am missing some (map) or (replace) iterative operator.
. Kanen Flowers http://kanen.me .

xytroxon
Posts: 296
Joined: Tue Nov 06, 2007 3:59 pm
Contact:

Re: Replacing HTML codes

Post by xytroxon »

Code: Select all

(setq text "Telefónica to Sell Spanish Assets")

; regex flag = 0
(replace "&#.*?;" text (char (int (2 -1 $it))) 0)
(println text)
;-> Telefónica to Sell Spanish Assets
(exit)
-- xytroxon
"Many computers can print only capital letters, so we shall not use lowercase letters."
-- Let's Talk Lisp (c) 1976

kanen
Posts: 145
Joined: Thu Mar 25, 2010 6:24 pm
Contact:

Re: Replacing HTML codes

Post by kanen »

kanen wrote:I feel like I am missing some (map) or (replace) iterative operator.
In other words, $it is the iterative operator I was missing. :)

d'oh!
xytroxon wrote:

Code: Select all

(setq text "Telefónica to Sell Spanish Assets")

; regex flag = 0
(replace "&#.*?;" text (char (int (2 -1 $it))) 0)
(println text)
;-> Telefónica to Sell Spanish Assets
(exit)
-- xytroxon
. Kanen Flowers http://kanen.me .

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Re: Replacing HTML codes

Post by Lutz »

In other words, $it is the iterative operator I was missing. :)
No, this one would work too:

Code: Select all

> (set 'x "Telefónica to Sell Spanish Assets")
"Telefónica to Sell Spanish Assets"
> (replace {&#(\d+);} x (char (int $1)) 0)
"Telefónica to Sell Spanish Assets"
> 
'$it' is just a replacement for '$0', 'replace' always iterates through all occurrences found:

http://www.newlisp.org/downloads/newlis ... ml#replace

and here about the anaphoric '$it':

http://www.newlisp.org/downloads/newlis ... em_symbols

xytroxon
Posts: 296
Joined: Tue Nov 06, 2007 3:59 pm
Contact:

Re: Replacing HTML codes

Post by xytroxon »

It's a little more complicated then what I posted from memory... Handling all the HTML special entity codes in html or rss docs is a pain!

Besides decimal codes, there are...

hexadecimal codes -> � ... ÿ ... &0x150; etc...

common codes -> & < > &nbsp; etc...

foreign language -> &iexcl; &iquest; &euro; etc...

http://tlt.its.psu.edu/suggestions/inte ... ehtml.html

http://webdesign.about.com/library/bl_htmlcodes.htm

A more general solution:

Code: Select all

(define (HTML-special-chars str)
; code here
)

(replace "&.*?;" text (HTML-special-chars $1) 0)
And I've seen &amp; in rss docs requiring a second pass...

-- xytroxon
"Many computers can print only capital letters, so we shall not use lowercase letters."
-- Let's Talk Lisp (c) 1976

Locked