Module: YARD::Templates::Helpers::MarkupHelper
- Included in:
- HtmlHelper
- Defined in:
- lib/yard/templates/helpers/markup_helper.rb
Overview
Helper methods for loading and managing markup types.
Constant Summary collapse
- MARKUP_PROVIDERS =
The default list of markup providers for each markup type
{ :markdown => [ {:lib => :rdiscount, :const => 'RDiscount'}, {:lib => :kramdown, :const => 'Kramdown::Document'}, {:lib => :bluecloth, :const => 'BlueCloth'}, {:lib => :maruku, :const => 'Maruku'}, {:lib => :redcarpet, :const => 'RedcarpetCompat'}, {:lib => :'rpeg-markdown', :const => 'PEGMarkdown'}, ], :textile => [ {:lib => :redcloth, :const => 'RedCloth'}, ], :rdoc => [ {:lib => nil, :const => 'YARD::Templates::Helpers::Markup::RDocMarkup'}, ], :ruby => [], :text => [], :html => [], :none => [], }
- MARKUP_EXTENSIONS =
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.
{ :html => ['htm', 'html', 'shtml'], :text => ['txt'], :textile => ['textile', 'txtile'], :markdown => ['markdown', 'md', 'mdown', 'mkd'], :rdoc => ['rdoc'], :ruby => ['rb', 'ru'] }
- MARKUP_FILE_SHEBANG =
Contains the Regexp object that matches the shebang line of extra files to detect the markup type.
/\A#!(\S+)\s*$/
Class Method Summary collapse
-
.clear_markup_cache ⇒ void
Clears the markup provider cache information.
Instance Method Summary collapse
-
#load_markup_provider(type = ) ⇒ Boolean
Attempts to load the first valid markup provider in MARKUP_PROVIDERS.
-
#markup_class(type = ) ⇒ Class
Gets the markup provider class/module constant for a markup type Call #load_markup_provider before using this method.
-
#markup_file_contents(contents) ⇒ String
deprecated
Deprecated.
Use CodeObjects::ExtraFileObject#contents instead
-
#markup_for_file(contents, filename) ⇒ Symbol
Checks for a shebang or looks at the file extension to determine the markup type for the file contents.
-
#markup_provider(type = ) ⇒ Symbol
Gets the markup provider name for a markup type Call #load_markup_provider before using this method.
Class Method Details
.clear_markup_cache ⇒ void
This method returns an undefined value.
Clears the markup provider cache information. Mainly used for testing.
10 11 12 |
# File 'lib/yard/templates/helpers/markup_helper.rb', line 10 def clear_markup_cache self.markup_cache = {} end |
Instance Method Details
#load_markup_provider(type = ) ⇒ Boolean
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.
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 |
# File 'lib/yard/templates/helpers/markup_helper.rb', line 72 def load_markup_provider(type = [: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 && [:markup_provider] providers = providers.select {|p| p[:lib] == [:markup_provider] } end if providers == nil || providers.empty? log.error "Invalid markup type '#{type}' or markup provider " + "(#{[: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 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 name, lib = *[providers.first[:const], providers.first[:lib] || type] log.error "Missing '#{lib}' gem for #{type.to_s.capitalize} formatting. Install it with `gem install #{lib}`" false end |
#markup_class(type = ) ⇒ Class
Gets the markup provider class/module constant for a markup type Call #load_markup_provider before using this method.
145 146 147 148 |
# File 'lib/yard/templates/helpers/markup_helper.rb', line 145 def markup_class(type = [:markup]) load_markup_provider(type) MarkupHelper.markup_cache[type][:class] end |
#markup_file_contents(contents) ⇒ String
Use CodeObjects::ExtraFileObject#contents instead
Strips any shebang lines on the file contents that pertain to markup or preprocessing data.
136 137 138 |
# File 'lib/yard/templates/helpers/markup_helper.rb', line 136 def markup_file_contents(contents) contents =~ MARKUP_FILE_SHEBANG ? $' : contents end |
#markup_for_file(contents, filename) ⇒ Symbol
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
118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/yard/templates/helpers/markup_helper.rb', line 118 def markup_for_file(contents, filename) if contents && contents =~ MARKUP_FILE_SHEBANG # Shebang support return $1.to_sym end ext = (File.extname(filename)[1..-1] || '').downcase MARKUP_EXTENSIONS.each do |type, exts| return type if exts.include?(ext) end [:markup] end |
#markup_provider(type = ) ⇒ Symbol
Gets the markup provider name for a markup type Call #load_markup_provider before using this method.
155 156 157 |
# File 'lib/yard/templates/helpers/markup_helper.rb', line 155 def markup_provider(type = [:markup]) MarkupHelper.markup_cache[type][:provider] end |