Search code examples
htmlemacsrendering

Rendering HTML code from EmacsLisp representation


I can parse the html using the libxml-parse-html-region function.

But I don't know how to render it back into HTML. The shr.el functions doesn't seem to do anything at all.

What I tried is this. I wrote an HTML string

<html>
  <head></head>
  <body></body>
</html>

and parsed it with libxml-parse-html-region function and pretty printed with dom-pp function. Like so

(dom-pp (libxml-parse-html-region (region-beginning) (region-end))))

This results in the following lisp representation

(html nil
 (head nil)
 (body nil))

being inserted into the buffer. Which is OK. The next step does not work. I want to render this lips representation back into the HTML representation. I tried to use the (shr-render-region) with (region-begining) and (region-end) function calls as arguments, like so

(shr-render-region (region-beginning) (region-end))

I mark a region and call the above code interactively. However, this does not do anything as far as I can tell. There is nothing inserted into the buffer and the shr-render-region does not return a string.

So how do I render the lisp representation back into HTML code?

Update 1: The shr-render-region renders HTML code into formated text, so it's not going to work.

Update 2: The shr.el functions won't work, it looks like they are for rendering HTML in an emacs buffer, but not rendering the code (the html string).


Solution

  • The dom-print function can be used to print the lisp representation back into HTML text.