Created
July 6, 2012 07:23
-
-
Save yokolet/3058641 to your computer and use it in GitHub Desktop.
Nokogiri Gem from Clojure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns tulip.sample) | |
(import '(org.jruby.embed ScriptingContainer LocalContextScope)) | |
(def c (ScriptingContainer. LocalContextScope/SINGLETHREAD)) | |
(def paths | |
[(str (System/getProperty "user.dir") "/../../resources/ruby/gems/nokogiri-1.5.3.rc4-java/lib")]) | |
(. c setLoadPaths paths) | |
(def script | |
" | |
require 'rubygems'; require 'nokogiri'; require 'open-uri' | |
doc = Nokogiri::HTML(open('http://clojuredocs.org/clojure_core')) | |
doc.to_java | |
") | |
(def dom (. c runScriptlet script)) | |
(defn node-list-seq | |
"from http://stackoverflow.com/questions/5898215/ | |
converting-a-org-w3c-dom-nodelist-to-a-clojure-iseq" | |
[^org.w3c.dom.NodeList node-list] | |
(map (fn [index] (.item node-list index)) | |
(range (.getLength node-list)))) | |
(println | |
(map #(.getNodeName %) | |
(node-list-seq (.. dom getDocumentElement getChildNodes)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment