lib/baurets/optionsful/documentator.rb in optionsful-0.1.6 vs lib/baurets/optionsful/documentator.rb in optionsful-0.1.7

- old
+ new

@@ -16,13 +16,17 @@ rescue => e [500, {}, e.backtrace.join("\n")] end end end - + private + require 'rubygems' + require 'yaml' + require 'RedCloth' + def extract_documentation(env) path = env["PATH_INFO"] path = path.gsub(@config.base_path, '') # do routing introspection: routes = ::Baurets::Optionsful::Introspections.do_routing_introspection @@ -42,25 +46,44 @@ controller_name = ::Baurets::Optionsful::Introspections.discover_controller_name(path_parts) + "_controller" file = File.join(RAILS_ROOT, "app", "controllers", controller_name + ".rb") controller_class = controller_name.camelize - service_doc = extract_comments_above(file, find_line_for(file, controller_class, :class)).join("\n") + service_doc = extract_comments_above(file, find_line_for(file, controller_class, :class)) methods_docs = [] controller_actions.each do |info| methods_docs << [info, extract_comments_above(file, find_line_for(file, info[1], :method)).join("\n")] end - body = "\n\nService: \n" + service_doc + "\n" - methods_docs.each do |md| - body += "\n\nMethod: #{md[0][0]} \n Action: #{md[0][1]} \n Metadata: \n #{md[1]}\n\n-- end ---\n" - end + body = build_html(service_doc, methods_docs) - [200, {}, "Under development!!! \n #{body}"] + [200, {"Content-Type" => "text/html"}, body] end + + def build_html(comment, methods) + comments = comment.join("\n").gsub(/^#+\s/, '') + resource = YAML::parse(comments) + html = "<html><head></head><body>" + resource_title = resource["resource"]["title"].value + + title = "h1. " + resource_title.to_s if resource_title + + html += RedCloth.new(title).to_html + + html += RedCloth.new("#{resource["resource"]["description"].value}").to_html + + methods.each do |meth| + meth_verb = meth[0][0] + #TODO + html += RedCloth.new("*" + meth_verb).to_html + end + html += "</body></html>" + html + end + def relate_action_to_method(path, verb) action = "" routes = ::Baurets::Optionsful::Introspections.do_routing_introspection route_guess = ::Baurets::Optionsful::Introspections.guess_route(routes, path) routes.each do |route| @@ -98,10 +121,10 @@ doc << line else line_number = 0 end end - doc.reverse + doc = doc.reverse doc end def find_line_for(file, name, type) lines = file_lines(file)