Created
December 13, 2013 17:59
-
-
Save alanhogan/7948390 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class Descss | |
def initialize(module_name) | |
# Modules output content, so they should include the .css extension. | |
# (This is why we don't look for module.sass, just module.css.sass.) | |
begin | |
file = File.open("app/assets/stylesheets/nri-modules/#{module_name}.css.sass") | |
rescue | |
file = File.open("app/assets/stylesheets/nri-modules/#{module_name}.css.scss") | |
end | |
@source = file.read | |
end | |
def method_missing (name, *args) | |
if name =~ /\Ahas_(.+)\?\Z/ | |
return ! self.send($1.to_sym).nil? | |
end | |
super | |
end | |
# Not DRY at all. TODO! (Maybe only parse it once.) | |
# And I realized these ||=’s are stupid because they will recalculate nils forever. | |
# Of course, that won't matter if we only parse the source once | |
def name | |
@name ||= begin | |
if documentation && documentation.match(%{<(b|strong)>([^<]+)<}) | |
$2 | |
else | |
nil | |
end | |
end | |
end | |
def documentation | |
@documentation ||= begin | |
match = @source.match(%r{/\*\s?doc\s*(.*?)\*/}m) | |
if match | |
Kramdown::Document.new(unindent match[1]).to_html.html_safe | |
else | |
nil | |
end | |
end | |
end | |
def example_html | |
@html ||= begin | |
if example_haml | |
Haml::Engine.new(example_haml).render.html_safe | |
else | |
nil | |
end | |
end | |
end | |
def example_haml | |
@haml ||= begin | |
match = @source.match(%r{/\*\s?ex(?:amples?)?\s*(.*?)\*/}m) | |
if match && match[1].present? | |
unindent match[1] | |
else | |
nil | |
end | |
end | |
end | |
def javascript | |
@javascript ||= begin | |
match = @source.match(%r{/\*\s?javascript\s*(.*?)\*/}m) | |
if match && match[1].present? | |
"<script>\n#{unindent match[1]}\n</script>".html_safe | |
else | |
nil | |
end | |
end | |
end | |
def states | |
@states ||= begin | |
match = @source.match(%r{/\*\s?states?\s*(.*?)\*/}m) | |
if match | |
states = match[1].split(/\s/m).reject(&:blank?) | |
return nil unless states.length > 0 | |
states | |
else | |
nil | |
end | |
end | |
end | |
def submodules | |
@submodules ||= begin | |
match = @source.match(%r{/\*\s?submodules?\s*(.*?)\*/}m) | |
if match | |
submodules = match[1].split(/\s/m).reject(&:blank?) | |
return nil unless submodules.length > 0 | |
submodules | |
else | |
nil | |
end | |
end | |
end | |
protected | |
def unindent(text) | |
text.split("\n").map { |line| line.sub(/\A /, '') }.join "\n" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment