lib/kramdown/options.rb in kramdown-2.4.0 vs lib/kramdown/options.rb in kramdown-2.5.0

- old
+ new

@@ -37,11 +37,11 @@ # Allowed option types. ALLOWED_TYPES = [String, Integer, Float, Symbol, Boolean, Object] @options = {} - @cached_defaults = nil + @defaults = nil # Define a new option called +name+ (a Symbol) with the given +type+ (String, Integer, Float, # Symbol, Boolean, Object), default value +default+ and the description +desc+. If a block is # specified, it should validate the value and either raise an error or return a valid value. # @@ -53,11 +53,11 @@ raise ArgumentError, "Option name #{name} is already used" if @options.key?(name) raise ArgumentError, "Invalid option type #{type} specified" unless ALLOWED_TYPES.include?(type) raise ArgumentError, "Invalid type for default value" if !(type === default) && !default.nil? raise ArgumentError, "Missing validator block" if type == Object && block.nil? @options[name] = Definition.new(name, type, default, desc, block) - @cached_defaults = nil + @defaults = nil end # Return all option definitions. def self.definitions @options @@ -68,15 +68,16 @@ @options.key?(name.to_sym) end # Return a Hash with the default values for all options. def self.defaults - @cached_defaults ||= begin - temp = {} - @options.each {|_n, o| temp[o.name] = o.default } - temp.freeze - end + @defaults ||= + begin + temp = {} + @options.each {|_n, o| temp[o.name] = o.default } + temp.freeze + end end # Merge the #defaults Hash with the *parsed* options from the given Hash, i.e. only valid option # names are considered and their value is run through the #parse method. def self.merge(hash) @@ -138,11 +139,11 @@ # - or an array. # # Optionally, the array is checked for the correct size. def self.simple_array_validator(val, name, size = nil) if String === val - val = val.split(/,/) + val = val.split(",") elsif !(Array === val) raise Kramdown::Error, "Invalid type #{val.class} for option #{name}" end if size && val.size != size raise Kramdown::Error, "Option #{name} needs exactly #{size} values" @@ -233,10 +234,19 @@ Default: '' Used by: HTML/Latex converter EOF + define(:header_links, Boolean, false, <<~EOF) + Adds anchor tags within headers that can be used to generate permalinks + when not using a table of contents. + + The anchor tags are empty, but can be styled to your liking. + + Default: false + EOF + define(:transliterated_header_ids, Boolean, false, <<~EOF) Transliterate the header text before generating the ID Only ASCII characters are used in headers IDs. This is not good for languages with many non-ASCII characters. By enabling this option @@ -348,25 +358,25 @@ EOF case val when String if val =~ /^(\d)\.\.(\d)$/ val = Range.new($1.to_i, $2.to_i).to_a - elsif val =~ /^\d(?:,\d)*$/ - val = val.split(/,/).map(&:to_i).uniq + elsif val.match?(/^\d(?:,\d)*$/) + val = val.split(",").map(&:to_i).uniq else raise Kramdown::Error, "Invalid syntax for option toc_levels" end when Array unless val.eql?(TOC_LEVELS_ARRAY) val = val.map(&:to_i).uniq end when Range - if val.eql?(TOC_LEVELS_RANGE) - val = TOC_LEVELS_ARRAY - else - val = val.map(&:to_i).uniq - end + val = if val.eql?(TOC_LEVELS_RANGE) + TOC_LEVELS_ARRAY + else + val.map(&:to_i).uniq + end else raise Kramdown::Error, "Invalid type #{val.class} for option toc_levels" end if val.any? {|i| !TOC_LEVELS_RANGE.cover?(i) } raise Kramdown::Error, "Level numbers for option toc_levels have to be integers from 1 to 6" @@ -583,9 +593,27 @@ in an ID! Default: '' Used by: HTML EOF + + define(:footnote_link_text, String, '%s', <<~EOF) do |val| + The text used for the footnote number in a footnote link + + This option can be used to add additional text to the footnote + link. It should be a format string, and is passed the footnote + number as the only argument to the format string. + e.g. "[footnote %s]" would display as "[footnote 1]". + + Default: '%s' + Used by: HTML + EOF + if !val.include?('%s') + raise Kramdown::Error, "option footnote_link_text needs to contain a '%s'" + end + val + end + define(:remove_line_breaks_for_cjk, Boolean, false, <<~EOF) Specifies whether line breaks should be removed between CJK characters Default: false