(PDOC-24) Clean up code for HTML generation

Prior to this commit there was a lot of duplicated code around
the generation of HTML for templates. Clean up and simplify code by
adding an HTMLHelper class with helper functions which can be used
in place of repeated logic.
This commit is contained in:
Hailee Kenney 2015-01-22 15:21:01 -08:00
parent 5f60c99bfd
commit 60b2802f04
7 changed files with 79 additions and 116 deletions

View File

@ -27,23 +27,7 @@
<p class="tag_title">Return:</p> <p class="tag_title">Return:</p>
<ul class="return"> <ul class="return">
<li> <li>
<span class ="type"> <%= @html_helper.generate_return_types(@class_details[:return][1], @class_details[:return][0]) %>
<% if !@class_details[:return][1].nil? %>
(<tt>
<% @class_details[:return][1].each do |type| %>
<%= type %>
<% if @class_details[:return][1].last != type %>
,
<% end %>
<% end %>
</tt>) —
<% end %>
</span>
<% if !@class_details[:return][0].nil? %>
<div class="inline">
<p><%= @class_details[:return][0] %></p>
</div>
<% end %>
</li> </li>
</ul> </ul>
<% end %> <% end %>

View File

@ -1,41 +1,6 @@
<h2>Parameter Summary</h2> <h2>Parameter Summary</h2>
<div class="tags"> <div class="tags">
<ul class="param"> <ul class="param">
<% @param_details.each do |param| %> <%= @html_helper.generate_parameters(@param_details) %>
<li>
<% if !param[:exists?] %>
<strike>
<% end %>
<span class="name"><%= param[:name] %></span>
<%# TODO: Linkify defaults that resolve to variable declarations in a different scope. %>
<span class="type">
<% if param[:types] %>
(<% param[:types].each do |type| %>
<tt>
<% if param[:types].last != type %>
<%= type %>,
<% else %>
<%= type %>
<% end %>
</tt>
<% end %>)
<% else %>
<tt>(TBD)</tt>
<% end %>
</span>
<% unless param[:fq_name].nil? %>
<tt><%= "=> #{param[:fq_name]}" %></tt>
<% end %>
<% if param[:desc]%>
<div class="inline">
<p><%= param[:desc] %></p>
</div>
<% end %>
<% if !param[:exists] %>
</strike>
<% end %>
</li>
<% end %>
</ul> </ul>
</div> </div>

View File

@ -1,5 +1,7 @@
include T('default/module') include T('default/module')
require File.join(File.dirname(__FILE__),'../html_helper')
def init def init
sections :header, :box_info, :pre_docstring, :docstring, :parameter_details sections :header, :box_info, :pre_docstring, :docstring, :parameter_details
end end
@ -7,6 +9,8 @@ end
def parameter_details def parameter_details
return if object.parameters.empty? return if object.parameters.empty?
@html_helper = HTMLHelper.new
param_tags = object.tags.find_all{ |tag| tag.tag_name == "param"} param_tags = object.tags.find_all{ |tag| tag.tag_name == "param"}
params = object.parameters params = object.parameters
@ -30,6 +34,8 @@ def header
end end
def docstring def docstring
@html_helper = HTMLHelper.new
examples = Hash.new examples = Hash.new
example_tags = object.tags.find_all { |tag| tag.tag_name == "example" } example_tags = object.tags.find_all { |tag| tag.tag_name == "example" }
example_tags.each do |example| example_tags.each do |example|

View File

@ -0,0 +1,53 @@
class HTMLHelper
def generate_return_types(types, desc = nil)
result = []
result << "(<span class=\"type\"><tt>" << types.join(", ") << "</tt></span>)"
if !desc.nil?
result << "- <div class=\"inline\"><p>#{desc}</p></div>"
end
result.join
end
def generate_parameters(params)
result = []
params.each do |param|
result << "<li>"
if !param[:exists?]
result << "<strike>"
end
result << "<span class=\"name\">#{param[:name]} </span>"
result << "<span class=\"type\">"
if param[:types]
result << "(" << "<tt>" << param[:types].join(", ") << "</tt>" << ")"
else
result << "(<tt>TBD</tt>)"
end
result << "</span>"
# This is only relevant for manifests, not puppet functions
unless param[:fq_name].nil?
result << "<tt> => #{param[:fq_name]}</tt>"
end
if param[:desc]
result << "- <div class=\"inline\"><p> #{param[:desc]} </p></div>"
end
if !param[:exists?]
result << "</strike>"
end
result << "</li>"
end
result.join
end
end

View File

@ -1,6 +1,12 @@
<h2>Function Details</h2>
<% @class_details.each do |func| %> <% @class_details.each do |func| %>
<h3 class="signature" id = <%= "#{func[:name]}-instance_method" %>> <h3 class="signature" id = <%= "#{func[:name]}-instance_method" %>>
<strong><%= func[:name] %> </strong> <strong>
<% if func[:return] %>
<%= @html_helper.generate_return_types(func[:return][1]) %>
<% end %>
<%= func[:name] %>
</strong>
</h3> </h3>
<div class="docstring"> <div class="docstring">
<div class="discussion"> <div class="discussion">
@ -31,23 +37,7 @@
<p class="tag_title">Returns:</p> <p class="tag_title">Returns:</p>
<ul class="return"> <ul class="return">
<li> <li>
<span class ="type"> <%= @html_helper.generate_return_types(func[:return][1], func[:return][0]) %>
<% if !func[:return][1].nil? %>
(<tt>
<% func[:return][1].each do |type| %>
<%= type %>
<% if func[:return][1].last != type %>
,
<% end %>
<% end %>
</tt>) —
<% end %>
</span>
<% if !func[:return][0].nil? %>
<div class="inline">
<p><%= func[:return][0] %></p>
</div>
<% end %>
</li> </li>
</ul> </ul>
<% end %> <% end %>
@ -55,39 +45,7 @@
<p class="tag_title">Parameters:</p> <p class="tag_title">Parameters:</p>
<div class="tags"> <div class="tags">
<ul class="param"> <ul class="param">
<% func[:params].each do |param| %> <%= @html_helper.generate_parameters(func[:params]) %>
<li>
<% if !param[:exists?] %>
<strike>
<% end %>
<span class="name"><%= param[:name] %></span>
<%# TODO: Linkify defaults that resolve to variable declarations in a different scope. %>
<span class="type">
<% if param[:types] %>
(<% param[:types].each do |type| %>
<tt>
<% if param[:types].last != type %>
<%= type %>,
<% else %>
<%= type %>
<% end %>
</tt>
<% end %>)
<% else %>
<tt>(TBD)</tt>
<% end %>
</span>
<% if param[:desc]%>
<div class="inline">
<p><%= param[:desc] %></p>
</div>
<% end %>
<% if !param[:exists] %>
</strike>
<% end %>
</li>
<% end %>
</ul> </ul>
</div> </div>
<% end %> <% end %>

View File

@ -4,18 +4,10 @@
<li class = "private"> <li class = "private">
<span class = "summary_signature"> <span class = "summary_signature">
<a href =<%= "##{method[:name]}-instance_method"%>> <a href =<%= "##{method[:name]}-instance_method"%>>
<span class ="type">
<% if ! method[:return_types].nil? %> <% if ! method[:return_types].nil? %>
(<tt> <%= @html_helper.generate_return_types(method[:return_types]) %>
<% method[:return_types].each do |type| %>
<%= type %>
<% if method[:return_types].last != type %>
,
<% end %>
<% end %>
</tt>) —
<% end %> <% end %>
</span> -
<strong><%= method[:name] %></strong> <strong><%= method[:name] %></strong>
</span> </span>
</li></a> </li></a>

View File

@ -1,9 +1,11 @@
include T('default/module') include T('default/module')
require File.join(File.dirname(__FILE__),'../html_helper')
def init def init
sections :header, :box_info, :pre_docstring, T('docstring'), sections :header, :box_info, :pre_docstring, T('docstring'),
:method_summary, [:item_summary], :method_summary, [:item_summary],
:method_details_list, :method_details :method_details_list, [T('method_details')]
@methods = object.children @methods = object.children
end end
@ -34,6 +36,7 @@ end
def method_summary def method_summary
@method_details = [] @method_details = []
@html_helper = HTMLHelper.new
@methods.each do |method| @methods.each do |method|
# If there are multiple sentences in the method description, only # If there are multiple sentences in the method description, only
@ -51,8 +54,9 @@ def method_summary
erb(:method_summary) erb(:method_summary)
end end
def method_details def method_details_list
@class_details = [] @class_details = []
@html_helper = HTMLHelper.new
@methods.each do |object| @methods.each do |object|
examples = Hash.new examples = Hash.new
@ -122,3 +126,4 @@ def extract_param_details(params_array, tags_hash)
parameter_info parameter_info
end end