|
# TODO |
|
# |
|
# * Find a way to add content without escaping. e.g. `<` escaped to `<`. |
|
# * How to deal with dynamic attributes, if necessary? |
|
|
|
# ---------------------------------------------------------------------------- # |
|
|
|
require 'nokogiri' |
|
|
|
def process_derb(file) |
|
doc = Nokogiri::HTML(File.read(file)) |
|
|
|
# A `data-derb` attribute replaces the content of the element with the ERB |
|
# specified in the attribute. |
|
doc.xpath('//*[@data-derb]').each do |element| |
|
element.content = element['data-derb'] |
|
element.remove_attribute('data-derb') |
|
end |
|
|
|
# A `data-derb-block` attribute in addition inserts `<% end %>` before the |
|
# closing tag of the element, which should be used for e.g. conditional blocks |
|
# or closures. |
|
doc.xpath('//*[@data-derb-block]').each do |element| |
|
element.prepend_child(Nokogiri::XML::Text.new(element['data-derb-block'], doc)) |
|
element.add_child(Nokogiri::XML::Text.new('<% end %>', doc)) |
|
element.remove_attribute('data-derb-block') |
|
end |
|
|
|
# Replace attributes prefixed with `derb-` for the same name without the |
|
# prefix. E.g. replace a static anchor `href` attribute with a dynamic one. |
|
doc.xpath('//@*[starts-with(name(), "derb-")]').each do |attribute| |
|
name = attribute.name.sub(/^derb-/, '') |
|
element = attribute.parent |
|
element[name] = attribute.content |
|
element.remove_attribute(attribute.name) |
|
end |
|
|
|
template = doc.to_s |
|
# Derp |
|
template = template.gsub('<', '<').gsub('>', '>').gsub('%20', ' ') |
|
#puts template |
|
template |
|
end |
|
|
|
# ---------------------------------------------------------------------------- # |
|
|
|
# Define `pod` variable that's available to template. |
|
class Pod |
|
def name |
|
'ARAnalytics' |
|
end |
|
|
|
def homepage |
|
'https://github.com/orta/ARAnalytics' |
|
end |
|
|
|
def summary |
|
'Simplify your iOS/Mac analytics' |
|
end |
|
|
|
# Optional |
|
def description |
|
#'ARAnalytics is to iOS what Analytical is to ruby, or Analytics.js is to javascript.' |
|
nil |
|
end |
|
|
|
# Multi-value |
|
def platforms |
|
[ |
|
'iOS', |
|
#'OS X', |
|
] |
|
end |
|
end |
|
pod = Pod.new |
|
|
|
require 'erb' |
|
puts ERB.new(process_derb('index.derb.html')).result(binding) |
And as I said before, I think anything beyond replacing element content is a Bad Idea® and People Who Edit This Will Get Confused And Fuck That Up™.
For example, one thing those pesky web designers always seem to want to be able to do is this:
How are you planning to support that (without adding a ton of additional markup that’s not needed)?