Renders documentations from result of collector
class Renderer
| (None) |
constructor: (@result, @options) -> theme = 'default' @resources_dir = resolve __dirname, '../themes', theme, 'resources' @templates_dir = resolve __dirname, '../themes', theme, 'templates'
Converts link markups to HTML links in the description
| Name | Type | Description |
|---|---|---|
rel_path | String | |
str | String |
| Name | Type | Description |
|---|---|---|
| (Returns) | String |
_convertLink: (rel_path, str) ->
return '' if not str
str = str.replace /\[\[#([^\[\]]+)\]\]/g, (_, $1) =>
if @result.ids[$1] and @result.ids[$1] isnt 'DUPLICATED ENTRY'
filename = @result.ids[$1].filename + '.html'
html_id = @result.ids[$1].html_id or ''
return "<a href='#{rel_path}#{filename}##{html_id}'>#{$1}</a>"
else
return @_makeMissingLink $1
return str| Name | Type | Description |
|---|---|---|
source | String | |
target | String | |
callback | Function() |
| (Nothing) |
_copyResources: (source, target, callback) ->
try
files = fs.readdirSync target
catch
files = []
for file in files
if file[0] isnt '.'
fs.rmrfSync resolve target, file
try fs.mkdirSync target
fs.copyRecursive source, target, ->
callback()Groups items by namespaces
| (None) |
| (Nothing) |
_groupByNamespaces: (items) ->
if items.length is 0
return []
current_group = []
grouped_items = [current_group]
current_namespace = items[0].namespace
items.forEach (item) ->
if current_namespace isnt item.namespace
current_group = []
grouped_items.push current_group
current_namespace = item.namespace
current_group.push item
return grouped_items| Name | Type | Description |
|---|---|---|
type | String |
| Name | Type | Description |
|---|---|---|
| (Returns) | String |
_makeMissingLink: (type, place = '') ->
txt = if @result.ids[type]
"'#{type}' link is ambiguous"
else
"'#{type}' link does not exist"
console.log txt + " #{place}"
return "<span class='missing-link'>#{type}</span>"| Name | Type | Description |
|---|---|---|
rel_path | String | |
str | String |
| Name | Type | Description |
|---|---|---|
| (Returns) | String |
_makeSeeLink: (rel_path, str) ->
if @result.ids[str]
filename = @result.ids[str].filename + '.html'
html_id = @result.ids[str].html_id or ''
str = "<a href='#{rel_path}#{filename}##{html_id}'>#{str}</a>"
return strMakes links for given type
| Name | Type | Description |
|---|---|---|
rel_path | String | |
type | String |
| Name | Type | Description |
|---|---|---|
| (Returns) | String |
_makeTypeLink: (rel_path, type, place = '') ->
return type if not type
getlink = (type) =>
if @options.types[type]
link = @options.types[type]
else if @result.ids[type] and @result.ids[type] isnt 'DUPLICATED ENTRY'
filename = @result.ids[type].filename + '.html'
html_id = @result.ids[type].html_id or ''
link = "#{rel_path}#{filename}##{html_id}"
else
return @_makeMissingLink type, place
return "<a href='#{link}'>#{type}</a>"
if res = type.match(/\[(.*)\]\((.*)\)/)
@options.types[res[1]] = res[2]
return "<a href='#{res[2]}'>#{res[1]}</a>"
if res = type.match /(.*?)<(.*)>/
return "#{@_makeTypeLink rel_path, res[1]}<#{@_makeTypeLink rel_path, res[2]}>"
else
return getlink typeRenders classes
| (None) |
| (Nothing) |
_renderClasses: ->
return if @result.classes.length is 0
try fs.mkdirSync "#{@options.output_dir}/classes"
pug_options =
rel_path: '../'
type: 'classes'
@_renderOne pug_options, 'class-toc', 'classes/index'
@result.classes.forEach (klass) =>
pug_options =
rel_path: '../'
name: klass.ctx.name
klass: klass
properties: klass.properties
type: 'classes'
_makeTypeLink: (path, type) =>
@_makeTypeLink path, type, "(in #{klass.full_path})"
@_renderOne pug_options, 'class', klass.filenameRenders features
| (None) |
| (Nothing) |
_renderFeatures: ->
return if @result.features.length is 0
try fs.mkdirSync "#{@options.output_dir}/features"
@result.features.forEach (feature) =>
pug_options =
rel_path: '../'
name: feature.name
feature: feature
type: 'features'
@_renderOne pug_options, 'feature', feature.filenameRenders files
| (None) |
| (Nothing) |
_renderFiles: ->
return if @result.files.length is 0
try fs.mkdirSync "#{@options.output_dir}/files"
@result.files.forEach (file) =>
pug_options =
rel_path: '../'
name: file.name
file: file
type: 'files'
@_renderOne pug_options, 'file', file.filenameRenders guides
| (None) |
| (Nothing) |
_renderGuides: ->
return if @result.guides.length is 0
try fs.mkdirSync "#{@options.output_dir}/guides"
@result.guides.forEach (guide) =>
pug_options =
rel_path: '../'
name: guide.name
content: guide.content
type: 'guides'
@_renderOne pug_options, 'extra', guide.filenameRenders modules
| (None) |
| (Nothing) |
_renderModules: ->
return if @result.modules.length is 0
try fs.mkdirSync "#{@options.output_dir}/modules"
pug_options =
rel_path: '../'
type: 'modules'
@_renderOne pug_options, 'module-toc', 'modules/index'
@result.modules.forEach (module) =>
pug_options =
rel_path: '../'
name: module.ctx.name
module_data: module
properties: module.properties
type: 'modules'
@_renderOne pug_options, 'module', module.filenameRenders one template
| (None) |
| (Nothing) |
_renderOne: (pug_options, template, output) ->
pug_options.result = @result
pug_options.makeTypeLink = @_makeTypeLink.bind(@) if not pug_options.makeTypeLink
pug_options.makeSeeLink = @_makeSeeLink.bind(@)
pug_options.convertLink = @_convertLink.bind(@)
pug_options.github = @options.github
pug_options.cache = true
pug_options.self = true
pug.renderFile "#{@templates_dir}/#{template}.pug", pug_options, (error, result) =>
return console.error error.stack if error
output_file = "#{@options.output_dir}/#{output}.html"
fs.writeFile output_file, result, (error) =>
return console.error 'failed to create '+output_file if error
console.log output_file + ' is created' if not @options.quietRenders pages
| (None) |
| (Nothing) |
_renderPages: ->
if @result.pages.length > 0
pug_options =
rel_path: './'
name: 'Pages'
type: 'pages'
@_renderOne pug_options, 'pages', 'pages'Renders REST apis
| (None) |
| (Nothing) |
_renderRESTApis: ->
if @result.restapis.length > 0
pug_options =
rel_path: './'
name: 'REST APIs'
type: 'restapis'
@_renderOne pug_options, 'restapis', 'restapis'Renders the README
| (None) |
| (Nothing) |
_renderReadme: ->
pug_options =
rel_path: './'
name: 'README'
content: @result.readme
type: 'home'
@_renderOne pug_options, 'extra', 'index'Runs
| (None) |
| (Nothing) |
run: ->
@result.ns_pages = @_groupByNamespaces @result.pages
@result.ns_restapis = @_groupByNamespaces @result.restapis
@result.ns_classes = @_groupByNamespaces @result.classes
@result.ns_modules = @_groupByNamespaces @result.modules
@result.ns_features = @_groupByNamespaces @result.features
@result.ns_files = @_groupByNamespaces @result.files
@_copyResources @resources_dir, @options.output_dir, =>
@_renderReadme()
@_renderGuides()
@_renderPages()
@_renderRESTApis()
@_renderClasses()
@_renderModules()
@_renderFeatures()
@_renderFiles()