Initial commit

This commit is contained in:
Mikko Ahlroth 2023-02-12 21:25:09 +02:00
commit 512acf457e
8 changed files with 4218 additions and 0 deletions

4
.gitignore vendored Normal file
View file

@ -0,0 +1,4 @@
*.beam
*.ez
build
erl_crash.dump

2
.tool-versions Normal file
View file

@ -0,0 +1,2 @@
gleam 0.26.2
erlang 25.2.2

19
LICENSE Normal file
View file

@ -0,0 +1,19 @@
Copyright (c) 2023 Mikko Ahlroth
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

34
README.md Normal file
View file

@ -0,0 +1,34 @@
# glentities
[![Package Version](https://img.shields.io/hexpm/v/glentities)](https://hex.pm/packages/glentities)
[![Hex Docs](https://img.shields.io/badge/hex-docs-ffaff3)](https://hexdocs.pm/glentities/)
An HTML entity encoder/decoder for Gleam.
## Quick start
```gleam
import glentities
glentities.encode("</html>", glentities.Named) // "&lt;&sol;html&gt;"
glentities.encode("</html>", glentities.Hex) // "&#x3C;&#x2F;html&#x3E;"
glentities.decode("&#x3C;&#x2F;html&#x3E;") // "</html>"
glentities.decode("&lt;&sol;html&gt;") // "</html>"
```
## Development
```sh
gleam test # Run the tests
```
## Installation
This package can be added to your Gleam project:
```sh
gleam add glentities
```
and its documentation can be found at <https://hexdocs.pm/glentities>.

16
gleam.toml Normal file
View file

@ -0,0 +1,16 @@
name = "glentities"
version = "1.0.0"
description = "HTML entity encoder/decoder for Gleam"
# Fill out these fields if you intend to generate HTML documentation or publish
# your project to the Hex package manager.
#
licences = ["MIT"]
repository = { type = "gitlab", user = "Nicd", repo = "glentities" }
links = []
[dependencies]
gleam_stdlib = "~> 0.26"
[dev-dependencies]
gleeunit = "~> 0.10"

11
manifest.toml Normal file
View file

@ -0,0 +1,11 @@
# This file was generated by Gleam
# You typically do not need to edit this file
packages = [
{ name = "gleam_stdlib", version = "0.26.1", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "B17BBE8A78F3909D93BCC6C24F531673A7E328A61F24222EB1E58D0A7552B1FE" },
{ name = "gleeunit", version = "0.10.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "ECEA2DE4BE6528D36AFE74F42A21CDF99966EC36D7F25DEB34D47DD0F7977BAF" },
]
[requirements]
gleam_stdlib = "~> 0.26"
gleeunit = "~> 0.10"

4064
src/glentities.gleam Normal file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,68 @@
import gleeunit
import gleeunit/should
import glentities
pub fn main() {
gleeunit.main()
}
pub fn decode_test() {
let input =
"This &amp &apos;&#115;tring&apos; contains &#128064; many &#xA66E; &quot;encoded&QUOT; characters. &#x2603;"
should.equal(
glentities.decode(input),
"This &amp 'string' contains 👀 many ꙮ \"encoded\" characters. ☃",
)
}
pub fn encode_named_test() {
let input =
"This &amp 'string' contains many Θ \"encoded\" characters. ☃"
should.equal(
glentities.encode(input, glentities.Named),
"This &amp;amp &apos;string&apos; contains &MediumSpace;&hairsp; many &Theta; &quot;encoded&quot; characters&period; ☃",
)
}
pub fn encode_hex_test() {
let input =
"This &amp 'string' contains many Θ \"encoded\" characters. ☃"
should.equal(
glentities.encode(input, glentities.Hex),
"This &#x26;amp &#x27;string&#x27; contains &#x205F;&#x200A; many &#x398; &#x22;encoded&#x22; characters&#x2E; &#x2603;",
)
}
pub fn roundtrip_named_test() {
let input =
normalise(
"Ἰοὺ ἰού· τὰ πάντʼ ἂν ἐξήκοι σαφῆ. ஸ்றீனிவாஸ ராமானுஜன் ஐயங்கார் A ∩ B = { c : c ∈ A, c ∈ B }",
)
should.equal(
input
|> glentities.encode(glentities.Named)
|> glentities.decode(),
input,
)
}
pub fn roundtrip_hex_test() {
let input =
normalise(
"Ἰοὺ ἰού· τὰ πάντʼ ἂν ἐξήκοι σαφῆ. ஸ்றீனிவாஸ ராமானுஜன் ஐயங்கார் A ∩ B = { c : c ∈ A, c ∈ B }",
)
should.equal(
input
|> glentities.encode(glentities.Hex)
|> glentities.decode(),
input,
)
}
external fn normalise(text: String) -> String =
"unicode" "characters_to_nfc_binary"