Module: YARD::Templates::Helpers::HtmlHelper

Defined in:
lib/yard-api/templates/helpers/html_helper.rb

Instance Method Summary collapse

Instance Method Details

#htmlify(text, markup = options.markup) ⇒ String

Turns text into HTML using markup style formatting.



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/yard-api/templates/helpers/html_helper.rb', line 101

def htmlify(text, markup = options.markup)
  markup_meth = "html_markup_#{markup}"
  return text unless respond_to?(markup_meth)
  return "" unless text
  return text unless markup
  html = send(markup_meth, text)
  if html.respond_to?(:encode)
    html = html.force_encoding(text.encoding) # for libs that mess with encoding
    html = html.encode(:invalid => :replace, :replace => '?')
  end
  html = resolve_links(html)
  html
end

override yard-appendix link_appendix



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/yard-api/templates/helpers/html_helper.rb', line 69

def link_appendix(ref)
  __errmsg = "unable to locate referenced appendix '#{ref}'"

  puts "looking up appendix: #{ref}"
  unless appendix = lookup_appendix(ref.to_s)
    raise __errmsg
  end

  topic, controller = *lookup_topic(appendix.namespace.to_s)

  unless topic
    raise __errmsg
  end

  html_file = "#{topicize topic.first}.html"
  bookmark = "#{appendix.name.to_s.gsub(' ', '+')}-appendix"
  link_url("#{html_file}##{bookmark}", appendix.title)
end


88
89
90
91
92
# File 'lib/yard-api/templates/helpers/html_helper.rb', line 88

def sidebar_link(title, href)
  "    <a href=\"\#{url_for(href)}\">\#{title}</a>\n  HTML\nend\n"

#static_pagesObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/yard-api/templates/helpers/html_helper.rb', line 14

def static_pages()
  options = YARD::APIPlugin.options

  paths = Array(options.static).map do |entry|
    pages = if entry.is_a?(Hash)
      glob = entry['glob']
      blacklist = Array(entry['exclude'])

      unless glob
        raise "Invalid static page entry, expected Hash to contain a 'glob' parameter: #{entry}"
      end

      pages = Dir.glob(entry['glob'])

      if blacklist.any?
        pages.delete_if { |p| blacklist.any? { |filter| p.match(filter) } }
      end

      pages
    elsif entry.is_a?(Array)
      entry.map(&:to_s)
    elsif entry.is_a?(String)
      [ entry ]
    end
  end.flatten.compact

  markdown_exts = YARD::Templates::Helpers::MarkupHelper::MARKUP_EXTENSIONS[:markdown]
  readme_page = options.readme
  pages = Dir.glob(paths)

  if readme_page.present?
    pages.delete_if { |page| page.match(readme_page) }
  end

  pages.map do |page|
    filename = 'file.' + File.split(page).last.sub(/\..*$/, '.html')

    # extract page title if it's a markdown document; title is expected to
    # be an h1 on the very first line:
    title = if markdown_exts.include?(File.extname(page).sub('.', ''))
      (File.open(page, &:gets) || '').strip.sub('# ', '')
    else
      # otherwise we'll just sanitize the file name
      File.basename(page).sub(/\.\w+$/, '').gsub(/\W/, ' ').gsub(/\s+/, ' ').capitalize
    end

    {
      src: page,
      filename: filename,
      title: title
    }
  end
end

#topicize(str) ⇒ Object



4
5
6
# File 'lib/yard-api/templates/helpers/html_helper.rb', line 4

def topicize(str)
  str.gsub(' ', '_').underscore
end

#url_for_file(filename, anchor = nil) ⇒ Object



8
9
10
11
12
# File 'lib/yard-api/templates/helpers/html_helper.rb', line 8

def url_for_file(filename, anchor = nil)
  link = filename.filename
  link += (anchor ? '#' + urlencode(anchor) : '')
  link
end