Skip to content

Commit

Permalink
Dev (#1)
Browse files Browse the repository at this point in the history
* Color.parse_color refactored

* Generated dev branch for dev

* Gamma right? Need more tests

* Gamma right!

* Working on doc

* Working on coverage

* Release v0.1.0
  • Loading branch information
RobertDober committed Oct 3, 2021
1 parent a53b3fb commit 9f22fcf
Show file tree
Hide file tree
Showing 19 changed files with 1,342 additions and 204 deletions.
166 changes: 149 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,108 @@ and any changes you make in this file will most likely be lost
All doctests in this README have been extracted from the code by [Extractly](https://github.com/RobertDober/extractly)
and are therefore tested

## TagCloud


[![CI](https://github.com/RobertDober/tag_cloud/actions/workflows/ci.yml/badge.svg)](https://github.com/RobertDober/tag_cloud/actions/workflows/ci.yml)
[![Coverage Status](https://coveralls.io/repos/github/RobertDober/tag_cloud/badge.svg?branch=master)](https://coveralls.io/github/RobertDober/tag_cloud?branch=master)
[![Hex.pm](https://img.shields.io/hexpm/v/tag_cloud.svg)](https://hex.pm/packages/tag_cloud)
[![Hex.pm](https://img.shields.io/hexpm/dw/tag_cloud.svg)](https://hex.pm/packages/tag_cloud)
[![Hex.pm](https://img.shields.io/hexpm/dt/tag_cloud.svg)](https://hex.pm/packages/tag_cloud)

- Make Tag Clouds from a simple DSL

e.g.
```
12 16 100 # translates to style="color: #000000; font-size: 16pt; font-weight: 100;"
#ffdd00 3em bb # style="color: #ffdd00; font-size: 3em; font-weight: 800;"
```

- Elixir Tools to create Tag clouds

- `TagCloud.Compiler.dsl_to_attributes`

```elixir
iex(1)> TagCloud.Compiler.dsl_to_attributes("12 16 100")
[{"style", "color: #000000; font-size: 16pt; font-weight: 100;"}]
```

- Independent Library Functions

- Gamma correction for scaled colors

- Earmark Integration (needs v1.4.16-pre2 or greater)

The most general way to integrate with Earmark is with `make_tag_clouds`

```elixir
iex(2)> markdown = [
...(2)> "Elixir %tc: 12 20 800", "",
...(2)> "Erlang %tc: 10/red 2em", "",
...(2)> "Phoenix %tc: 8/sandybrown" ]
...(2)> render_html(markdown)
...(2)> markdown
...(2)> |> Earmark.as_ast!(annotations: "%tc:", inner_html: true)
...(2)> |> make_tag_clouds
...(2)> |> Earmark.transform
"<span style=\"color: #000000; font-size: 20pt; font-weight: 800;\">\nElixir </span>\n<span style=\"color: #ff7171; font-size: 2em;\">\nErlang </span>\n<span style=\"color: #ed6d00;\">\nPhoenix </span>\n"
```

We can render to html directly with `render_html`, which is a shortcut for the above

```elixir
iex(3)> markdown = [
...(3)> "Elixir %tc: 12 20 800", "",
...(3)> "Erlang %tc: 10/red 2em", "",
...(3)> "Phoenix %tc: 8/sandybrown" ]
...(3)> render_html(markdown)
"<span style=\"color: #000000; font-size: 20pt; font-weight: 800;\">\nElixir </span>\n<span style=\"color: #ff7171; font-size: 2em;\">\nErlang </span>\n<span style=\"color: #ed6d00;\">\nPhoenix </span>\n"
```


Or just transform the AST

```elixir
iex(4)> markdown = [
...(4)> "Elixir %tc: 12 20 800", "",
...(4)> "Erlang %tc: 10/red 2em", "",
...(4)> "Phoenix %tc: 8/sandybrown" ]
...(4)> render_ast(markdown)
[
{"span", [{"style", "color: #000000; font-size: 20pt; font-weight: 800;"}], ["Elixir "], %{annotation: "%tc: 12 20 800"}},
{"span", [{"style", "color: #ff7171; font-size: 2em;"}], ["Erlang "], %{annotation: "%tc: 10/red 2em"}},
{"span", [{"style", "color: #ed6d00;"}], ["Phoenix "], %{annotation: "%tc: 8/sandybrown"}}
]
```

which is a shortcut for this

```elixir
iex(5)> markdown = [
...(5)> "Elixir %tc: 12 20 800", "",
...(5)> "Erlang %tc: 10/red 2em", "",
...(5)> "Phoenix %tc: 8/sandybrown" ]
...(5)> markdown
...(5)> |> Earmark.as_ast!(annotations: "%tc:", inner_html: true)
...(5)> |> make_tag_clouds
[
{"span", [{"style", "color: #000000; font-size: 20pt; font-weight: 800;"}], ["Elixir "], %{annotation: "%tc: 12 20 800"}},
{"span", [{"style", "color: #ff7171; font-size: 2em;"}], ["Erlang "], %{annotation: "%tc: 10/red 2em"}},
{"span", [{"style", "color: #ed6d00;"}], ["Phoenix "], %{annotation: "%tc: 8/sandybrown"}}
]
```


## TagCloud.version/0

A convenience method to access this library's version

```elixir
iex(6)> {:ok, _} = Version.parse(version())
```


### TagCloud.Cli

usage:
Expand All @@ -17,36 +119,66 @@ usage:

convert file from Markdown to HTML.using Earmark and allowing for TagCloud annotations

where options can be any of:
cond do

none so far
file ends with .eex -> treat it as an EEx template

true -> treat file as plain markdown

### TagCloud


[![CI](https://github.com/RobertDober/tag_cloud/actions/workflows/ci.yml/badge.svg)](https://github.com/RobertDober/tag_cloud/actions/workflows/ci.yml)
[![Coverage Status](https://coveralls.io/repos/github/RobertDober/tag_cloud/badge.svg?branch=master)](https://coveralls.io/github/RobertDober/tag_cloud?branch=master)
[![Hex.pm](https://img.shields.io/hexpm/v/tag_cloud.svg)](https://hex.pm/packages/tag_cloud)
[![Hex.pm](https://img.shields.io/hexpm/dw/tag_cloud.svg)](https://hex.pm/packages/tag_cloud)
[![Hex.pm](https://img.shields.io/hexpm/dt/tag_cloud.svg)](https://hex.pm/packages/tag_cloud)
### TagCloud.version/0

Elixir Tools to create Tag clouds - Integration with Earmark Annotations - Independent Library Functions - Gamma correction for grey scales
A convenience method to access this library's version

```elixir
iex(6)> {:ok, _} = Version.parse(version())
```

- Earmark Integration (needs v1.4.16-pre2 or greater)

Although there are different ways the simplest way is to annotate your markdown document with tag_clouds and then render as follows
### TagCloud.Compiler

Compiles tag cloud specifications of the form
`color font_size font_weight color` to corresponding HTML attributes

#### TagCloud.Compiler.dsl_to_attributes/1

Implements the compilation

### Gray Scale

```elixir
annotated_markdown
|> Earmark.as_ast!
|> TagCloud.make_tag_clouds
|> Earmark.transform
iex(1)> dsl_to_attributes("10 12 100")
[{"style", "color: #717171; font-size: 12pt; font-weight: 100;"}]
```

### TagCloud.version/0
### Scale on Predefined Colors

All 140 color names defined by the CSS standard are supported.
The complete list can be found [here](https://en.wikipedia.org/wiki/Web_colors#Extended_colors)

```elixir
iex(2)> dsl_to_attributes("8/fuchsia 3em 800")
[{"style", "color: #ff9bff; font-size: 3em; font-weight: 800;"}]
```


## TagCloud.EarmarkAst

An Earmark AST processor which will change annotated tag cloud paragraphs into spans with the necessary attributes

Needs Earmark version 1.4.16-pre2 or later

E.g.

```elixir
iex(1)> markdown = [
...(1)> "Elixir %tc: 10/blue 18 800", "",
...(1)> "Ruby %tc: 4/red 10 100"]
...(1)> render_html(markdown)
"<span style=\"color: #7171ff; font-size: 18pt; font-weight: 800;\">\nElixir </span>\n<span style=\"color: #ffd4d4; font-size: 10pt; font-weight: 100;\">\nRuby </span>\n"
```

A convenience method to access this library's version


## Author
Expand Down
9 changes: 8 additions & 1 deletion README.md.eex
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@
All doctests in this README have been extracted from the code by [Extractly](https://github.com/RobertDober/extractly)
and are therefore tested

<%= xtra.moduledoc "TagCloud", include: :all, headline: 2, wrap_code_blocks: "elixir" %>

<%= xtra.moduledoc "TagCloud.Cli", include: :all, headline: 3, wrap_code_blocks: "elixir" %>

<%= xtra.moduledoc "TagCloud", include: :all, headline: 3, wrap_code_blocks: "elixir" %>
<%= xtra.functiondoc :all, module: "TagCloud", headline: 3, wrap_code_blocks: "elixir" %>

<%= xtra.moduledoc "TagCloud.Compiler", inlcude: :all, headline: 3, wrap_code_blocks: "elixir" %>
<%= xtra.functiondoc :all, module: "TagCloud.Compiler", headline: 4, wrap_code_blocks: "elixir" %>

<%= xtra.moduledoc "TagCloud.EarmarkAst", inlcude: :all, headline: 2, wrap_code_blocks: "elixir" %>

## Author

Expand Down
6 changes: 6 additions & 0 deletions coveralls.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"skip_files": [
"tag_cloud/cli.ex",
"test/support"
]
}
117 changes: 107 additions & 10 deletions lib/tag_cloud.ex
Original file line number Diff line number Diff line change
@@ -1,36 +1,133 @@
defmodule TagCloud do
@moduledoc """
@moduledoc ~S"""
[![CI](https://github.com/RobertDober/tag_cloud/actions/workflows/ci.yml/badge.svg)](https://github.com/RobertDober/tag_cloud/actions/workflows/ci.yml)
[![Coverage Status](https://coveralls.io/repos/github/RobertDober/tag_cloud/badge.svg?branch=master)](https://coveralls.io/github/RobertDober/tag_cloud?branch=master)
[![Hex.pm](https://img.shields.io/hexpm/v/tag_cloud.svg)](https://hex.pm/packages/tag_cloud)
[![Hex.pm](https://img.shields.io/hexpm/dw/tag_cloud.svg)](https://hex.pm/packages/tag_cloud)
[![Hex.pm](https://img.shields.io/hexpm/dt/tag_cloud.svg)](https://hex.pm/packages/tag_cloud)
Elixir Tools to create Tag clouds - Integration with Earmark Annotations - Independent Library Functions - Gamma correction for grey scales
- Make Tag Clouds from a simple DSL
e.g.
```
12 16 100 # translates to style="color: #000000; font-size: 16pt; font-weight: 100;"
#ffdd00 3em bb # style="color: #ffdd00; font-size: 3em; font-weight: 800;"
```
- Elixir Tools to create Tag clouds
- `TagCloud.Compiler.dsl_to_attributes`
iex(1)> TagCloud.Compiler.dsl_to_attributes("12 16 100")
[{"style", "color: #000000; font-size: 16pt; font-weight: 100;"}]
- Independent Library Functions
- Gamma correction for scaled colors
- Earmark Integration (needs v1.4.16-pre2 or greater)
Although there are different ways the simplest way is to annotate your markdown document with tag_clouds and then render as follows
The most general way to integrate with Earmark is with `make_tag_clouds`
iex(2)> markdown = [
...(2)> "Elixir %tc: 12 20 800", "",
...(2)> "Erlang %tc: 10/red 2em", "",
...(2)> "Phoenix %tc: 8/sandybrown" ]
...(2)> render_html(markdown)
...(2)> markdown
...(2)> |> Earmark.as_ast!(annotations: "%tc:", inner_html: true)
...(2)> |> make_tag_clouds
...(2)> |> Earmark.transform
"<span style=\"color: #000000; font-size: 20pt; font-weight: 800;\">\nElixir </span>\n<span style=\"color: #ff7171; font-size: 2em;\">\nErlang </span>\n<span style=\"color: #ed6d00;\">\nPhoenix </span>\n"
We can render to html directly with `render_html`, which is a shortcut for the above
iex(3)> markdown = [
...(3)> "Elixir %tc: 12 20 800", "",
...(3)> "Erlang %tc: 10/red 2em", "",
...(3)> "Phoenix %tc: 8/sandybrown" ]
...(3)> render_html(markdown)
"<span style=\"color: #000000; font-size: 20pt; font-weight: 800;\">\nElixir </span>\n<span style=\"color: #ff7171; font-size: 2em;\">\nErlang </span>\n<span style=\"color: #ed6d00;\">\nPhoenix </span>\n"
Or just transform the AST
iex(4)> markdown = [
...(4)> "Elixir %tc: 12 20 800", "",
...(4)> "Erlang %tc: 10/red 2em", "",
...(4)> "Phoenix %tc: 8/sandybrown" ]
...(4)> render_ast(markdown)
[
{"span", [{"style", "color: #000000; font-size: 20pt; font-weight: 800;"}], ["Elixir "], %{annotation: "%tc: 12 20 800"}},
{"span", [{"style", "color: #ff7171; font-size: 2em;"}], ["Erlang "], %{annotation: "%tc: 10/red 2em"}},
{"span", [{"style", "color: #ed6d00;"}], ["Phoenix "], %{annotation: "%tc: 8/sandybrown"}}
]
which is a shortcut for this
iex(5)> markdown = [
...(5)> "Elixir %tc: 12 20 800", "",
...(5)> "Erlang %tc: 10/red 2em", "",
...(5)> "Phoenix %tc: 8/sandybrown" ]
...(5)> markdown
...(5)> |> Earmark.as_ast!(annotations: "%tc:", inner_html: true)
...(5)> |> make_tag_clouds
[
{"span", [{"style", "color: #000000; font-size: 20pt; font-weight: 800;"}], ["Elixir "], %{annotation: "%tc: 12 20 800"}},
{"span", [{"style", "color: #ff7171; font-size: 2em;"}], ["Erlang "], %{annotation: "%tc: 10/red 2em"}},
{"span", [{"style", "color: #ed6d00;"}], ["Phoenix "], %{annotation: "%tc: 8/sandybrown"}}
]
Of course not annotated blocks are not effected
iex(6)> markdown = [
...(6)> "Elixir %tc: 12 20 800", "",
...(6)> "Erlang", "",
...(6)> "Phoenix %tc: 8/sandybrown" ]
...(6)> render_ast(markdown)
[
{"span", [{"style", "color: #000000; font-size: 20pt; font-weight: 800;"}], ["Elixir "], %{annotation: "%tc: 12 20 800"}},
{"p", [], ["Erlang"], %{}},
{"span", [{"style", "color: #ed6d00;"}], ["Phoenix "], %{annotation: "%tc: 8/sandybrown"}}
]
And different annotations can be used, but than `make_tag_clouds` becomes a _NOP_
iex(7)> markdown = [
...(7)> "Elixir %%%: 12 20 800", "",
...(7)> "Erlang %%%: 10/red 2em", "",
...(7)> "Phoenix %%%: 8/sandybrown" ]
...(7)> markdown
...(7)> |> Earmark.as_ast!(annotations: "%%%:", inner_html: true)
...(7)> |> make_tag_clouds
[
{"p", [], ["Elixir "], %{annotation: "%%%: 12 20 800"}},
{"p", [], ["Erlang "], %{annotation: "%%%: 10/red 2em"}},
{"p", [], ["Phoenix "], %{annotation: "%%%: 8/sandybrown"}}
]
```elixir
annotated_markdown
|> Earmark.as_ast!
|> TagCloud.make_tag_clouds
|> Earmark.transform
```
"""


defdelegate dsl_to_attributes(description), to: TagCloud.Compiler

defdelegate make_tag_clouds(ast, options \\ [annotation: "%tc:"]), to: TagCloud.EarmarkAst

defdelegate make_atts_from_description(description), to: TagCloud.Compiler
defdelegate render_ast(input), to: TagCloud.EarmarkAst

defdelegate render_html(input), to: TagCloud.EarmarkAst

@doc """
A convenience method to access this library's version
iex(8)> {:ok, _} = Version.parse(version())
"""
@spec version :: binary()
def version do
:application.ensure_started(:tag_cloud)
with {:ok, version} = :application.get_key(:tag_cloud, :vsn), do: to_string(version)
end
end
# SPDX-License-Identifier: Apache-2.0
7 changes: 2 additions & 5 deletions lib/tag_cloud/cli/implementation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ defmodule TagCloud.Cli.Implementation do
none so far
"""

@cli_options ~W[
]

@spec _parse_args(binaries()) :: parse_result()
defp _parse_args(argv) do
switches = [
Expand All @@ -43,7 +40,7 @@ defmodule TagCloud.Cli.Implementation do
{ [ {:help, true} ], _, _ } -> :help
{ [ {:version, true} ], _, _ } -> :version
{ _options, [ file ], _ } -> file
{ options, [ ], _ } -> {:stderr, "need a file argument"}
{ _options, [ ], _ } -> {:stderr, "need a file argument"}
end
end

Expand All @@ -68,7 +65,7 @@ defmodule TagCloud.Cli.Implementation do
try do
{:stdio, _transform_markdown(file)}
rescue
e in RuntimeError -> {:stderr, to_string(e.message)}
e in File.Error -> {:stderr, Exception.message(e)}
end
end

Expand Down

0 comments on commit 9f22fcf

Please sign in to comment.