Created
July 15, 2010 15:13
-
-
Save thejefflarson/477085 to your computer and use it in GitHub Desktop.
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
# this.coffee | |
parse: (source, code) -> | |
lines: code.split '\n' | |
sections: [] | |
language: get_language source | |
has_code: docs_text: code_text: '' | |
save: (docs, code) -> | |
sections.push { | |
docs_text: docs | |
code_text: code | |
} | |
for line in lines | |
if line.match language.comment_matcher | |
if has_code | |
save docs_text, code_text | |
has_code: docs_text: code_text: '' | |
docs_text += line.replace(language.comment_matcher, '') + '\n' | |
else | |
has_code: true | |
code_text += line + '\n' | |
save docs_text, code_text | |
sections | |
_/ | |
_/_/_/ _/ _/_/ _/_/_/ _/_/_/ _/_/ | |
_/ _/ _/ _/ _/ _/ _/ _/ | |
_/ _/ _/ _/ _/ _/ _/ _/ | |
_/_/_/ _/ _/_/ _/_/_/ _/_/_/ _/_/ () | |
;; this.clj | |
(defn parse | |
"Given a string of code parse it into sections" | |
[source code] | |
(let [lines (.split code "\n") | |
language (get-language source) | |
save (fn | |
[sections line] | |
(conj sections | |
(merge-with (fn [left right] (.concat left right)) (pop sections) line))) | |
reducer (fn | |
[sections line] | |
(if (line :docs-text) | |
(if ((peek sections) :code-text) | |
(conj sections line) | |
(save sections line)) | |
(save sections line))) | |
mapper (fn | |
[line] | |
(if (re-find (language :comment-matcher)) | |
({:docs-text (.concat (replace-by (language :comment-matcher) (fn [m] "") line) "\n")}) | |
({:code-text (.concat line "\n")})))] | |
(reduce reducer [] (map mapper lines)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment