-
-
Notifications
You must be signed in to change notification settings - Fork 404
/
Copy pathmarkup_helper.rb
173 lines (157 loc) · 6.46 KB
/
markup_helper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# frozen_string_literal: true
require 'rubygems'
module YARD
module Templates::Helpers
# Helper methods for loading and managing markup types.
module MarkupHelper
class << self
# Clears the markup provider cache information. Mainly used for testing.
# @return [void]
def clear_markup_cache
self.markup_cache = {}
end
# @return [Hash{Symbol=>{(:provider,:class)=>Object}}] the cached markup providers
# @private
# @since 0.6.4
attr_accessor :markup_cache
end
MarkupHelper.clear_markup_cache
# The default list of markup providers for each markup type
MARKUP_PROVIDERS = {
:markdown => [
{:lib => :redcarpet, :const => 'RedcarpetCompat'},
{:lib => :rdiscount, :const => 'RDiscount'},
{:lib => :kramdown, :const => 'Kramdown::Document'},
{:lib => :bluecloth, :const => 'BlueCloth'},
{:lib => :maruku, :const => 'Maruku'},
{:lib => :'rpeg-markdown', :const => 'PEGMarkdown'},
{:lib => :rdoc, :const => 'YARD::Templates::Helpers::Markup::RDocMarkdown'},
{:lib => :commonmarker, :const => 'CommonMarker'}
],
:textile => [
{:lib => :redcloth, :const => 'RedCloth'}
],
:textile_strict => [
{:lib => :redcloth, :const => 'RedCloth'}
],
:rdoc => [
{:lib => nil, :const => 'YARD::Templates::Helpers::Markup::RDocMarkup'}
],
:org => [
{:lib => :'org-ruby', :const => 'Orgmode::Parser'}
],
:asciidoc => [
{:lib => :asciidoctor, :const => 'Asciidoctor'}
],
:ruby => [],
:text => [],
:pre => [],
:html => [],
:none => []
}
# Returns a list of extensions for various markup types. To register
# extensions for a type, add them to the array of extensions for the
# type.
# @since 0.6.0
MARKUP_EXTENSIONS = {
:html => ['htm', 'html', 'shtml'],
:text => ['txt'],
:textile => ['textile', 'txtile'],
:asciidoc => ['asciidoc', 'ad', 'adoc', 'asc'],
:markdown => ['markdown', 'md', 'mdown', 'mkd'],
:rdoc => ['rdoc'],
:org => ['org'],
:ruby => ['rb', 'ru']
}
# Contains the Regexp object that matches the shebang line of extra
# files to detect the markup type.
MARKUP_FILE_SHEBANG = /\A#!(\S+)\s*$/
# Attempts to load the first valid markup provider in {MARKUP_PROVIDERS}.
# If a provider is specified, immediately try to load it.
#
# On success this sets `@markup_provider` and `@markup_class` to
# the provider name and library constant class/module respectively for
# the loaded provider.
#
# On failure this method will inform the user that no provider could be
# found and exit the program.
#
# @return [Boolean] whether the markup provider was successfully loaded.
def load_markup_provider(type = options.markup)
return true if MarkupHelper.markup_cache[type]
MarkupHelper.markup_cache[type] ||= {}
providers = MARKUP_PROVIDERS[type.to_sym]
return true if providers && providers.empty?
if providers && options.markup_provider
providers = providers.select {|p| p[:lib] == options.markup_provider }
end
if providers.nil? || providers.empty?
log.error "Invalid markup type '#{type}' or markup provider " \
"(#{options.markup_provider}) is not registered."
return false
end
# Search for provider, return the library class name as const if found
providers.each do |provider|
begin require provider[:lib].to_s; rescue LoadError; next end if provider[:lib]
begin klass = eval("::" + provider[:const]); rescue NameError; next end # rubocop:disable Lint/Eval
MarkupHelper.markup_cache[type][:provider] = provider[:lib] # Cache the provider
MarkupHelper.markup_cache[type][:class] = klass
return true
end
# Show error message telling user to install first potential provider
lib = providers.first[:lib] || type
log.error "Missing '#{lib}' gem for #{type.to_s.capitalize} formatting. Install it with `gem install #{lib}`"
false
end
# Checks for a shebang or looks at the file extension to determine
# the markup type for the file contents. File extensions are registered
# for a markup type in {MARKUP_EXTENSIONS}.
#
# A shebang should be on the first line of a file and be in the form:
#
# #!markup_type
#
# Standard markup types are text, html, rdoc, markdown, textile
#
# @param [String] contents Unused. Was necessary prior to 0.7.0.
# Newer versions of YARD use {CodeObjects::ExtraFileObject#contents}
# @return [Symbol] the markup type recognized for the file
# @see MARKUP_EXTENSIONS
# @since 0.6.0
def markup_for_file(contents, filename)
return $1.to_sym if contents && contents =~ MARKUP_FILE_SHEBANG # Shebang support
ext = (File.extname(filename)[1..-1] || '').downcase
MARKUP_EXTENSIONS.each do |type, exts|
return type if exts.include?(ext)
end
options.markup
end
# Strips any shebang lines on the file contents that pertain to
# markup or preprocessing data.
#
# @deprecated Use {CodeObjects::ExtraFileObject#contents} instead
# @return [String] the file contents minus any preprocessing tags
# @since 0.6.0
def markup_file_contents(contents)
contents =~ MARKUP_FILE_SHEBANG ? $' : contents
end
# Gets the markup provider class/module constant for a markup type
# Call {#load_markup_provider} before using this method.
#
# @param [Symbol] type the markup type (:rdoc, :markdown, etc.)
# @return [Class] the markup class
def markup_class(type = options.markup)
load_markup_provider(type)
MarkupHelper.markup_cache[type][:class]
end
# Gets the markup provider name for a markup type
# Call {#load_markup_provider} before using this method.
#
# @param [Symbol] type the markup type (:rdoc, :markdown, etc.)
# @return [Symbol] the markup provider name (usually the gem name of the library)
def markup_provider(type = options.markup)
MarkupHelper.markup_cache[type][:provider]
end
end
end
end