class Sept
Markup as it should have been Write your page as s-expression structure (html
(head (title Hello World demo)) (body (p Hello Wrold!) ("p class='para'" Attributes are also supported in this form) (table (tr (th What) (th Coolness %)) (tr (td HTML) (td 0%)) (tr (td SEPT) (td %{param})))))
Learn more at:
Public Class Methods
Constructor. The only method you would need
@param params [Hash] Hash with parameters. @params files_to_parse [Array] List of files program will try to parse
# File lib/sept.rb, line 30 def initialize(params, files_to_parse, doctype, must_use_extension) @params = params @doctype = doctype @must_use_extension = must_use_extension @html = '' puts "@params: #{params}\n@doctype: #{@doctype}\n@must_use_extension: #{must_use_extension}\nfiles_to_parse: #{files_to_parse}" files_to_parse.each { |f| self.cook f } end
Recursive function that turns everything in `node` to a string
@param node [Array, String] A Sept
node
# File lib/sept.rb, line 69 def self.prepare(node) node.each_with_index do |sub, i| if sub.is_a? Array Sept.prepare sub else node[i] = sub.to_s end end end
Function that unfolds dot-notation and hash-notation p.class.class-to#id onclick=“…” 'p class=“class class-too” id=“id” onclick=“…”' id must be after .classes
@param tag [String] String like `tag.class.class2#id other-arg=“”` @return [String] Unfolded tag
# File lib/sept.rb, line 129 def self.unfold_tag(tag) halves = tag.split(' ') half = halves[0] id = '' if half.include? '#' temp = half.split('#') id = temp[-1] half = temp[0..-2].join('') end klass = '' if half.include? '.' temp = half.split('.') klass = temp[1..-1].join(' ') half = temp[0] end ret = half ret << " class='#{klass}'" unless klass.empty? ret << " id='#{id}'" unless id.empty? ret << " #{halves[1]}" unless halves[1].nil? ret end
Public Instance Methods
Function that 'cooks' passed file. It logs some info. The name is bad
@param filename [String] Name of file to 'cook'
# File lib/sept.rb, line 51 def cook(filename) @html = @doctype file = File.read filename puts "Got #{filename}:\n#{file}" filename = self.make_name filename file = self.generate file # Create file if it does not exist yet File.new filename, 'w+' unless File.file? filename File.write filename, file puts "Parsed and saved in #{filename}:\n#{file}" end
Method that chains lots of methods
@param file [String] Sept
file @return [String] HTML file
# File lib/sept.rb, line 44 def generate(file) self.to_html Sept.prepare SXP.read file % @params end
Function to make correct filename
@param [String] Filename @return [String] New filename
# File lib/sept.rb, line 83 def make_name(filename) if @must_use_extension if File.extname(filename) == '.septh' "#{filename[0..-6]}html" else "#{filename[0..-6]}css" end else File.basename filename, File.extname(filename) end end
Recursive function that generates HTML string and handles `#include`
@param node [Array, String] A Sept
node @return [String]
# File lib/sept.rb, line 99 def to_html(node) if node.is_a? Array if node.length == 1 @html << "<#{node[0]}/>" else if node[0] == "#include" file = self.generate File.read node[1].to_s node = [] node[0] = file else temp = Sept.unfold_tag node[0] @html << "<#{temp}>" node[1..-1].each { |e| self.to_html e } @html << "</#{temp.split(' ')[0]}>" end end else @html << node end @html end