From c308c995b647ac042555c9ea48047dcc7596d5cf Mon Sep 17 00:00:00 2001 From: Wojtek Mach Date: Tue, 13 Jul 2021 20:32:30 +0200 Subject: [PATCH] Add simple shell lexer (#1380) --- README.md | 22 ++++++++++---------- lib/ex_doc/application.ex | 6 ++++++ lib/ex_doc/shell_lexer.ex | 43 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 11 deletions(-) create mode 100644 lib/ex_doc/shell_lexer.ex diff --git a/README.md b/README.md index 851e7d571..6f2fd810b 100644 --- a/README.md +++ b/README.md @@ -94,22 +94,22 @@ You can ExDoc via the command line as follows: 1. Install ExDoc as an escript: - ```bash - $ mix escript.install hex ex_doc - ``` + ```bash + $ mix escript.install hex ex_doc + ``` 2. Then you are ready to use it in your projects. First, move into your project directory and make sure it is already compiled: - ```bash - $ cd PATH_TO_YOUR_PROJECT - $ mix compile - ``` + ```bash + $ cd PATH_TO_YOUR_PROJECT + $ mix compile + ``` -3. Next invoke the ex_doc executable from your project: +3. Next invoke the `ex_doc` executable from your project: - ```bash - $ ex_doc "PROJECT_NAME" "PROJECT_VERSION" path/to/project/ebin -m "PROJECT_MODULE" -u "https://github.com/GITHUB_USER/GITHUB_REPO" -l path/to/logo.png - ``` + ```bash + $ ex_doc "PROJECT_NAME" "PROJECT_VERSION" path/to/project/ebin -m "PROJECT_MODULE" -u "https://github.com/GITHUB_USER/GITHUB_REPO" -l path/to/logo.png + ``` For example, here are some acceptable values: diff --git a/lib/ex_doc/application.ex b/lib/ex_doc/application.ex index 193c1451e..ffa374d62 100644 --- a/lib/ex_doc/application.ex +++ b/lib/ex_doc/application.ex @@ -3,6 +3,12 @@ defmodule ExDoc.Application do use Application def start(_type, _args) do + Makeup.Registry.register_lexer(ExDoc.ShellLexer, + options: [], + names: ["shell", "sh", "bash", "zsh"], + extensions: [] + ) + Enum.each([:eex, :ex_unit, :iex, :logger, :mix], &Application.load/1) Supervisor.start_link([ExDoc.Refs], strategy: :one_for_one) end diff --git a/lib/ex_doc/shell_lexer.ex b/lib/ex_doc/shell_lexer.ex new file mode 100644 index 000000000..c55824e52 --- /dev/null +++ b/lib/ex_doc/shell_lexer.ex @@ -0,0 +1,43 @@ +defmodule ExDoc.ShellLexer do + # Makeup lexer for sh, bash, etc commands. + # The only thing it does is making the `$ ` prompt not selectable. + @moduledoc false + + @behaviour Makeup.Lexer + + @impl true + def lex(text, _opts) do + text + |> String.split("\n") + |> Enum.flat_map(fn + "$ " <> rest -> + [ + {:generic_prompt, %{selectable: false}, "$ "}, + {:text, %{}, rest <> "\n"} + ] + + text -> + [{:text, %{}, text <> "\n"}] + end) + end + + @impl true + def match_groups(_arg0, _arg1) do + raise "not implemented yet" + end + + @impl true + def postprocess(_arg0, _arg1) do + raise "not implemented yet" + end + + @impl true + def root(_arg0) do + raise "not implemented yet" + end + + @impl true + def root_element(_arg0) do + raise "not implemented yet" + end +end