You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Greg Johnston <gr...@gmail.com> on 2011/06/17 20:01:41 UTC

Parsing HTML in CouchApp show functions?

Hi all,

I'm trying to transform some HTML I have stored in a CouchDB document
to another format. It's actually pretty simple to do, really just
looping over the DOM. Normally I'd start out with something like:

var div = document.createElement("div");
div.innerHTML = [...]

...and be ready to go. Unfortunately it looks like "document" is
undefined in the show function. Am I doing something stupid here, is
there a better way, or does anyone have other suggestions?

Thanks,
Greg Johnston

Re: Parsing HTML in CouchApp show functions?

Posted by Justin Walgran <jw...@azavea.com>.
Show functions are not run in a browser, so you do not have access to a DOM.
You will need to parse the HTML as a string.

If you have the option, it may be easier to reverse your design and store
JSON in your CouchDB documents then render HTML in a show function, rather
than the other way around.

If you are with decoding HTML, John Resig wrote a SAX-style parser that you
could bundle into your show function:

http://ejohn.org/blog/pure-javascript-html-parser/

Justin

On Fri, Jun 17, 2011 at 2:01 PM, Greg Johnston <gr...@gmail.com>wrote:

> Hi all,
>
> I'm trying to transform some HTML I have stored in a CouchDB document
> to another format. It's actually pretty simple to do, really just
> looping over the DOM. Normally I'd start out with something like:
>
> var div = document.createElement("div");
> div.innerHTML = [...]
>
> ...and be ready to go. Unfortunately it looks like "document" is
> undefined in the show function. Am I doing something stupid here, is
> there a better way, or does anyone have other suggestions?
>
> Thanks,
> Greg Johnston
>

Re: Parsing HTML in CouchApp show functions?

Posted by Sean Copenhaver <se...@gmail.com>.
You should keep in mind that CouchDB is not a browser and JavaScript is just
a language with really no notion of a standard library. The browser hosts
the DOM and JavaScript, so you use JavaScript to interact with the DOM.

CouchDB does not come with a DOM or HTML parser library. You can still use
CommonJS modules and require them in your functions if you find a good
library. Hopefully someone can suggest one as I do not know.

http://wiki.apache.org/couchdb/CommonJS_Modules

I would say that it seems odd to me you would want to store the actual HTML
in CouchDB and not just the data. Then use shows to render the data into the
format of choice. Of course I have no idea what your situation is.

On Fri, Jun 17, 2011 at 2:01 PM, Greg Johnston <gr...@gmail.com>wrote:

> Hi all,
>
> I'm trying to transform some HTML I have stored in a CouchDB document
> to another format. It's actually pretty simple to do, really just
> looping over the DOM. Normally I'd start out with something like:
>
> var div = document.createElement("div");
> div.innerHTML = [...]
>
> ...and be ready to go. Unfortunately it looks like "document" is
> undefined in the show function. Am I doing something stupid here, is
> there a better way, or does anyone have other suggestions?
>
> Thanks,
> Greg Johnston
>



-- 
“The limits of language are the limits of one's world. “ -Ludwig von
Wittgenstein