Skip to content

Instantly share code, notes, and snippets.

@thejefflarson
Created July 15, 2010 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thejefflarson/477085 to your computer and use it in GitHub Desktop.
Save thejefflarson/477085 to your computer and use it in GitHub Desktop.
# 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