Replacing HTML codes

Started by kanen, April 17, 2011, 12:25:17 PM

Previous topic - Next topic

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.



(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[/url] .

xytroxon

#1

(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

#2
Quote from: "kanen"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!


Quote from: "xytroxon"
(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[/url] .

Lutz

#3
QuoteIn other words, $it is the iterative operator I was missing. :)


No, this one would work too:


> (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/newlisp_manual.html#replace">http://www.newlisp.org/downloads/newlis ... ml#replace">http://www.newlisp.org/downloads/newlisp_manual.html#replace



and here about the anaphoric '$it':



http://www.newlisp.org/downloads/newlisp_manual.html#system_symbols">http://www.newlisp.org/downloads/newlis ... em_symbols">http://www.newlisp.org/downloads/newlisp_manual.html#system_symbols

xytroxon

#4
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/international/web/codehtml.html">http://tlt.its.psu.edu/suggestions/inte ... ehtml.html">http://tlt.its.psu.edu/suggestions/international/web/codehtml.html



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



A more general solution:



(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