Skip to content

Commit

Permalink
Fixes: #403;
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertDober committed Feb 21, 2021
1 parent 100c194 commit 58bb9c4
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 13 deletions.
14 changes: 8 additions & 6 deletions RELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@

- fixing picture links in README.eex

- [403-warn-if-illegal-keyword-options-are-provided](https://github.com/robertdober/earmark_parser/issues/403)

- [401-option-to-disable-html-escapes](https://github.com/robertdober/earmark_parser/pull/401-to-disable-html-escapes)
Kudos to [paradox460](https://github.com/paradox460)

- [397-typo fix](https://github.com/robertdober/earmark_parser/pull/397)
Kudos to [rubysolo](https://github.com/rubysolo)

- [398-walk-ast-helper-and-ast-postprocessing-option](https://github.com/robertdober/earmark_parser/issues/398)

- [397-typo fix](https://github.com/robertdober/earmark_parser/pull/397)
Kudos to [rubysolo](https://github.com/rubysolo)

# 1.4.13 2020-12-03

- A minor fix for the compact output option , Noo Issue, OMG ;)

# 1.4.12 2020-11-27
Expand All @@ -27,10 +29,10 @@ Adapted the CLI to accept the wikilinks switch for `EarmarkParser`
# 1.4.11 2020-11-26

- [394-compact-html-output](https://github.com/robertdober/earmark_parser/pull/394)
Kudos and Спасибо to [rinpatch](https://github.com/rinpatch)
Kudos and Спасибо to [rinpatch](https://github.com/rinpatch)

- [387-treat-del-tag-as-compact-tag](https://github.com/pragdave/earmark/pull/387)
Kudos to [Médi-Rémi Hashim](https://github.com/mediremi)
Kudos to [Médi-Rémi Hashim](https://github.com/mediremi)

- [383-smartyants-inside-code-blocks](https://github.com/robertdober/earmark_parser/issues/-)
Kudos to [David Bernheisel](https://github.com/dbernheisel)
Expand Down
12 changes: 5 additions & 7 deletions lib/earmark.ex
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,10 @@ defmodule Earmark do
def as_html(lines, options \\ %Options{})

def as_html(lines, options) when is_list(options) do
as_html(lines, struct(Options, options))
case Options.make_options(options) do
{:ok, options1} -> as_html(lines, options1)
{:error, messages} -> {:error, "", messages}
end
end

def as_html(lines, options) do
Expand Down Expand Up @@ -225,12 +228,7 @@ defmodule Earmark do
Otherwise it behaves exactly as `as_html`.
"""
def as_html!(lines, options \\ %Options{})

def as_html!(lines, options) when is_list(options) do
as_html!(lines, struct(Options, options))
end

def as_html!(lines, options = %Options{}) do
def as_html!(lines, options) do
{_status, html, messages} = as_html(lines, options)
emit_messages(messages, options)
html
Expand Down
4 changes: 4 additions & 0 deletions lib/earmark/message.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ defmodule Earmark.Message do
messages
|> Enum.each(&emit_message(file, &1))
end
def emit_messages(messages, _) do
messages
|> Enum.each(&emit_message("<args>", &1))
end

@doc """
For final output
Expand Down
33 changes: 33 additions & 0 deletions lib/earmark/options.ex
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,43 @@ defmodule Earmark.Options do
end
end

@doc false
def make_options(options) do
legal_keys =
__MODULE__
|> struct()
|> Map.keys
|> MapSet.new

given_keys =
options
|> Keyword.keys
|> MapSet.new

violators =
MapSet.difference(given_keys, legal_keys)

if MapSet.size(violators) == 0 do
{:ok, struct(__MODULE__, options)}
else
{:error, _format_errors(violators, options)}
end
end

@doc false
def plugin_for_prefix(options, plugin_name) do
Map.get(options.plugins, plugin_name, false)
end

defp _format_error(violator, options) do
{:warning, 0, "Unrecognized option #{violator}: #{Keyword.get(options, violator) |> inspect()} ignored"}
end

defp _format_errors(violators, options) do
violators
|> Enum.map(&_format_error(&1, options))
end

end

# SPDX-License-Identifier: Apache-2.0
30 changes: 30 additions & 0 deletions test/acceptance/html/illegal_options_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
defmodule Test.Acceptance.Html.IllegalOptionsTest do
use ExUnit.Case

import ExUnit.CaptureIO
import Earmark, only: [as_html: 2, as_html!: 2]

describe "unrecognized options" do
test "with empty" do
messages = [
{:warning, 0, "Unrecognized option no_such_option: true ignored"}
]
assert as_html("", no_such_option: true) == {:error, "", messages}
end
test "with non empty" do
messages = [
{:warning, 0, "Unrecognized option hello: 42 ignored"},
{:warning, 0, "Unrecognized option no_such_option: true ignored"},
]
assert as_html("hello", no_such_option: true, hello: 42) == {:error, "", messages}
end
end

test "with as_html!" do
error_messages =
capture_io(:stderr, fn ->
as_html!("hello", oops: Earmark)
end)
assert error_messages == "<args>:0: warning: Unrecognized option oops: Earmark ignored\n"
end
end

0 comments on commit 58bb9c4

Please sign in to comment.