;;AUTOLOAD: html:unquote-values
;;AUTODOC: html:unquote-values "unquote characters in www URLs"
;; (html:unquote-values string)
;;
;; returns a new string with the forms %xx are expanded into the character of
;; hexadecimal code xx
;;e.g:
;; (html:unquote-values "a%5Eb")       ==> "a^b"

(setq html:unquote-values-re (regcomp "^([^%]*)%([0-9a-fA-F][0-9a-fA-F])"))

(defun html:unquote-values (string &aux
    (res (copy ""))
    (offset 0)
  )
  (while (html:unquote-values-re string offset)
    (nconc res (html:unquote-values-re 1))
    (put res -1 (xtoi (+ "0x" (html:unquote-values-re 2))))
    (setq offset (1 (get html:unquote-values-re 2)))
  )
  (nconc res (subseq string offset))
  res
)

;;AUTOLOAD: html:quote-values
;;AUTODOC: html:quote-values "quote characters in www URLs"
;; (html:quote-values string)
;;
;; returns a new string quoting chars invalid in URLs in the form %xx
;; we quote chars -) , : ;-@ [-^ ` {-
;; e.g:
;; (html:uote-values "A new program!")   ==> "A%20new%20program%21"

(setq html:quote-values-validchars "-+*.-9A-Z_a-z")
(setq html:quote-values-re (regcomp (+
      "^([" html:quote-values-validchars "]*)([^"
      html:quote-values-validchars "])"
)))
(defun html:quote-values (string &aux
    (res (copy ""))
    (offset 0)
  )
  (while (html:quote-values-re string offset)
    (nconc res (regsub html:quote-values-re 1))
    (with (cval (get (html:quote-values-re 2) 0))
      (put res -1 #\%)
      (put res -1 (get (itox (/ cval 16)) 2))
      (put res -1 (get (itox (mod cval 16)) 2))
    )
    (setq offset (1 (get html:quote-values-re 2)))
  )
  (nconc res (subseq string offset))
  res
)

;;AUTOLOAD: html:unquote-text
;;AUTODOC: html:unquote-text "unquote characters in html text"
;; (html:unquote-text string)
;;
;; returns a new string with the entities expanded
;;e.g:
;; (html:unquote-text "a&gt;b")       ==> "a>b"

(setq html:unquote-text-re (regcomp "^&([A-Za-z][-A-Za-z0-9._]*);"))

(defun html:unquote-text (string &aux
    (res (copy string))
  )
  (replace-string res
    "&(([A-Za-z][-A-Za-z0-9._]*)|#([0-9]+)|#x([0-9a-fA-F]+));"
    html:unquote-text-replace :all t :quote t
  )
)

(defun html:unquote-text-replace (re)
  (if (getn re 2)
    (with (name (re 2))
      (if					; first, Predefined entities
	(= name "lt") (copy "<")
	(= name "gt") (copy ">")
	(= name "amp") (copy "&")
	(= name "apos") (copy "'")
	(= name "quot") (copy "\"")
	t name
      )
    )
    (getn re 3)              ; decimal
    (String (list (Int (re 3))))
    (getn re 4)              ; hexa
    (String (list (xtoi (re 4))))
    t (re 0)
  )
)

;;AUTOLOAD: html:quote-text
;;AUTODOC: html:quote-text "quote characters in html text"
;; (html:quote-text string)
;;
;; returns a new string quoting chars invalid in HTML as entities
;; we quote chars & < > ' " as &amp; &lt; &gt; &apos; &quot;
;; e.g:
;; (html:quote-text "A <new> program!")   ==> "A&lt;new&gt;program!"

(defun html:quote-text (string &aux
    (res (copy string))
  )
  (replace-string res "&<>'\"" 
    ["&" "&amp;"  "<" "&lt;" ">" "&gt;" "'" "&apos;" "\"" "&quot;"]
    :all t :quote t
  )
)
