options.ex (3375B)
1 defmodule EarmarkParser.Options do 2 # What we use to render 3 defstruct renderer: EarmarkParser.HtmlRenderer, 4 # Inline style options 5 all: false, 6 gfm: true, 7 gfm_tables: false, 8 breaks: false, 9 footnotes: false, 10 footnote_offset: 1, 11 wikilinks: false, 12 parse_inline: true, 13 14 # allow for annotations 15 annotations: nil, 16 # additional prefies for class of code blocks 17 code_class_prefix: nil, 18 19 # Filename and initial line number of the markdown block passed in 20 # for meaningful error messages 21 file: "<no file>", 22 line: 1, 23 # [{:error|:warning, lnb, text},...] 24 messages: MapSet.new([]), 25 pure_links: true, 26 sub_sup: false, 27 28 # deprecated 29 pedantic: false, 30 smartypants: false, 31 timeout: nil 32 33 @doc false 34 def add_deprecations(options, messages) 35 36 def add_deprecations(%__MODULE__{smartypants: true} = options, messages) do 37 add_deprecations( 38 %{options | smartypants: false}, 39 [ 40 {:deprecated, 0, 41 "The smartypants option has no effect anymore and will be removed in EarmarkParser 1.5"} 42 | messages 43 ] 44 ) 45 end 46 47 def add_deprecations(%__MODULE__{timeout: timeout} = options, messages) when timeout != nil do 48 add_deprecations( 49 %{options | timeout: nil}, 50 [ 51 {:deprecated, 0, 52 "The timeout option has no effect anymore and will be removed in EarmarkParser 1.5"} 53 | messages 54 ] 55 ) 56 end 57 58 def add_deprecations(%__MODULE__{pedantic: true} = options, messages) do 59 add_deprecations( 60 %{options | pedantic: false}, 61 [ 62 {:deprecated, 0, 63 "The pedantic option has no effect anymore and will be removed in EarmarkParser 1.5"} 64 | messages 65 ] 66 ) 67 end 68 69 def add_deprecations(_options, messages), do: messages 70 71 @doc ~S""" 72 Use normalize before passing it into any API function 73 74 iex(1)> options = normalize(annotations: "%%") 75 ...(1)> options.annotations 76 ~r{\A(.*)(%%.*)} 77 """ 78 def normalize(options) 79 80 def normalize(%__MODULE__{} = options) do 81 case options.annotations do 82 %Regex{} -> 83 options 84 85 nil -> 86 options 87 88 _ -> 89 %{ 90 options 91 | annotations: Regex.compile!("\\A(.*)(#{Regex.escape(options.annotations)}.*)") 92 } 93 end 94 |> _set_all_if_applicable() 95 |> _deprecate_old_messages() 96 end 97 98 def normalize(options), do: struct(__MODULE__, options) |> normalize() 99 100 defp _deprecate_old_messages(opitons) 101 defp _deprecate_old_messages(%__MODULE__{messages: %MapSet{}} = options), do: options 102 103 defp _deprecate_old_messages(%__MODULE__{} = options) do 104 %{ 105 options 106 | messages: 107 MapSet.new([ 108 {:deprecated, 0, 109 "messages is an internal option that is ignored and will be removed from the API in v1.5.1"} 110 ]) 111 } 112 end 113 114 defp _set_all_if_applicable(options) 115 116 defp _set_all_if_applicable(%{all: true} = options) do 117 %{options | breaks: true, footnotes: true, gfm_tables: true, sub_sup: true, wikilinks: true} 118 end 119 120 defp _set_all_if_applicable(options), do: options 121 end 122 123 # SPDX-License-Identifier: Apache-2.0