Class: Markdown::Config
- Inherits:
-
Object
- Object
- Markdown::Config
- Defined in:
- lib/markdown/config.rb
Constant Summary collapse
- DEFAULT_EXTNAMES =
[ '.markdown', '.m', '.mark', '.mkdn', '.md', '.mdown', '.markdn', '.txt', '.text' ]
- DEFAULT_REDCARPET =
{ 'extensions' => [ 'no_intra_emphasis', 'fenced_code_blocks', 'tables', 'strikethrough' ] }
- DEFAULT_FILTERS =
[ 'comments-percent-style' ]
- DEFAULTS =
note: make kramdown default engine
{ 'libs' => [ 'kramdown' ], # note: make kramdown default engine 'extnames' => DEFAULT_EXTNAMES, 'redcarpet' => DEFAULT_REDCARPET, # todo/fix: merge nested hash?? 'filters' => DEFAULT_FILTERS }
- DEFAULTS_SERVICE =
pandoc-ruby - how to include - gemfile cannot install binary ?? rpeg-markdown - build failure - still active, anyway? rdiscount - # compilation error on heroku; sorry excluded for now
{ 'libs' => [ 'kramdown', # note: make kramdown default engine 'maruku', 'bluecloth', 'redcarpet' ], 'extnames' => DEFAULT_EXTNAMES, 'redcarpet' => DEFAULT_REDCARPET }
Instance Method Summary collapse
-
#dump ⇒ Object
for debugging dump all settings.
-
#initialize ⇒ Config
constructor
A new instance of Config.
- #known_markdown_libs ⇒ Object
- #load_props ⇒ Object
- #load_props_service ⇒ Object
- #markdown_extnames ⇒ Object
- #markdown_filters ⇒ Object
- #markdown_lib ⇒ Object
- #markdown_lib=(lib) ⇒ Object
- #markdown_lib_defaults(lib = nil) ⇒ Object
- #markdown_libs ⇒ Object
- #markdown_to_html_method(lib = nil) ⇒ Object
- #markdown_version_method(lib = nil) ⇒ Object
- #require_markdown_libs ⇒ Object
Constructor Details
#initialize ⇒ Config
Returns a new instance of Config.
82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/markdown/config.rb', line 82 def initialize # for an example see ./boot.rb if $MARKDOWN_USE_SERVICE_CONFIG == true load_props_service else load_props end @libs = [] require_markdown_libs() end |
Instance Method Details
#dump ⇒ Object
for debugging dump all settings
96 97 98 99 100 101 102 103 104 105 |
# File 'lib/markdown/config.rb', line 96 def dump # for debugging dump all settings puts "Markdown settings:" @props_default.dump if @props_default @props_home.dump if @props_home @props_work.dump if @props_work puts puts "Markdown libs:" puts " #{@libs.length} Markdown #{(@libs.length == 1) ? 'library' : 'libraries'} found: #{@libs.join(', ')}" end |
#known_markdown_libs ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/markdown/config.rb', line 115 def known_markdown_libs # returns an array of known markdown engines e.g. # [ 'pandoc-ruby', 'rdiscount', 'rpeg-markdown', 'maruku', 'bluecloth', 'kramdown' ] ## todo: allow single lib para instead of libs ## todo: allow ENV setting markdown_[select]_lib=xxxx ## todo/fix: use lookup with config parent cascade ## lookup order ## 1) env variable MARKDOWN_LIB ## 2) lib property (single markdown engine) ## 3) libs property (first-come first-serve markdown engine list) user_lib = Env.markdown_lib || @props.fetch( 'lib', nil ) if user_lib.nil? user_libs = @props.fetch( 'libs', nil ) else [ user_lib ] # return as array (wrap single lib entry) end end |
#load_props ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/markdown/config.rb', line 56 def load_props @props = @props_default = Props.new( DEFAULTS, 'DEFAULTS' ) # check for user settings (markdown.yml) in home folder ## todo: use .markdown.yml?? or differnt name ?? props_home_file = File.join( Env.home, 'markdown.yml' ) if File.exists?( props_home_file ) puts "Loading home settings from '#{props_home_file}'..." @props = @props_home = Props.load_file( props_home_file, @props ) end # check for user settings (markdown.yml) in working folder props_work_file = File.join( '.', 'markdown.yml' ) if File.exists?( props_work_file ) puts "Loading work settings from '#{props_work_file}'..." @props = @props_work = Props.load_file( props_work_file, @props ) end end |
#load_props_service ⇒ Object
77 78 79 80 |
# File 'lib/markdown/config.rb', line 77 def load_props_service puts "Loading service settings..." @props = @props_default = Props.new( DEFAULTS_SERVICE, 'DEFAULTS' ) end |
#markdown_extnames ⇒ Object
107 108 109 |
# File 'lib/markdown/config.rb', line 107 def markdown_extnames @props.fetch( 'extnames', nil ) end |
#markdown_filters ⇒ Object
111 112 113 |
# File 'lib/markdown/config.rb', line 111 def markdown_filters @props.fetch( 'filters', nil ) end |
#markdown_lib ⇒ Object
173 174 175 |
# File 'lib/markdown/config.rb', line 173 def markdown_lib @libs.first end |
#markdown_lib=(lib) ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/markdown/config.rb', line 154 def markdown_lib=( lib ) # fix/todo: check if @libs.first == lib => do nothing; return # check if value exists in libs array # if yes put it into first position # otherwise issue warning/error - better throw exception; engine not found # try to delete obj = @libs.delete( lib ) if obj.nil? # nothing deleted; no obj found # try to require; will raise load error exception if not found; know what your're doing! no fallback; sorry; better fail fast require lib end # add it back; make it first entry @libs.unshift( lib ) end |
#markdown_lib_defaults(lib = nil) ⇒ Object
181 182 183 184 185 |
# File 'lib/markdown/config.rb', line 181 def markdown_lib_defaults( lib=nil ) lib = @libs.first if lib.nil? ## todo: return props ? that acts like a hash?? (lets us support section lookup without deep merge???) opts = @props.fetch( lib, {} ) end |
#markdown_libs ⇒ Object
177 178 179 |
# File 'lib/markdown/config.rb', line 177 def markdown_libs @libs # NB: return all libs - should we return a clone? end |
#markdown_to_html_method(lib = nil) ⇒ Object
193 194 195 196 197 |
# File 'lib/markdown/config.rb', line 193 def markdown_to_html_method( lib=nil ) lib = @libs.first if lib.nil? method = @props.fetch_from_section( lib, 'converter', "#{lib.downcase}_to_html" ) # default to <lib>_to_html if converter prop not found method.tr('-','_').to_sym end |
#markdown_version_method(lib = nil) ⇒ Object
187 188 189 190 191 |
# File 'lib/markdown/config.rb', line 187 def markdown_version_method( lib=nil ) lib = @libs.first if lib.nil? method = "#{lib.downcase}_version" # default to <lib>_to_html if converter prop not found method.tr('-','_').to_sym end |
#require_markdown_libs ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/markdown/config.rb', line 139 def require_markdown_libs # check for available markdown libs/gems # try to require each lib and remove any not installed known_markdown_libs.each do |lib| begin require lib @libs << lib rescue LoadError => ex ## todo: use logger.debug instead of puts puts "Markdown library #{lib} not found. Use gem install #{lib} to install." end end end |