Skip to content

Commit

Permalink
Add ability to override the encoding (#29)
Browse files Browse the repository at this point in the history
* Add ability to override the encoding

* Fix option handling for multiple options.

* Add example for `generate/2`
  • Loading branch information
alappe authored and joshnuss committed Nov 14, 2018
1 parent 3a400fd commit 88f1c7f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,27 @@ Outputs
<html>Hello, world!</html>
```

### Encoding

While the output is always UTF-8 and has to be converted in another place, you can override the encoding statement in the xml declaration
with the `encoding` option:

```elixir
import XmlBuilder

[XmlBuilder.element(:oldschool, [])]
|> XmlBuilder.document()
|> XmlBuilder.generate(encoding: "ISO-8859-1")
|> :unicode.characters_to_binary(:unicode, :latin1)
```

Outputs

```xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<oldschool/>
```

### Formatting

With indentation: `XmlBuilder.generate(doc, format: :indent)`
Expand Down
9 changes: 7 additions & 2 deletions lib/xml_builder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,17 @@ defmodule XmlBuilder do
iex> XmlBuilder.generate({:name, nil, [{:first, nil, "Steve"}]})
"<name>\\n <first>Steve</first>\\n</name>"
iex> XmlBuilder.generate(:xml_decl, encoding: "ISO-8859-1")
~s|<?xml version="1.0" encoding="ISO-8859-1"?>|
"""
def generate(any, options \\ []),
do: format(any, 0, options) |> IO.chardata_to_string

defp format(:xml_decl, 0, _options),
do: ~s|<?xml version="1.0" encoding="UTF-8"?>|
defp format(:xml_decl, 0, options) do
encoding = Keyword.get(options, :encoding, "UTF-8")
~s|<?xml version="1.0" encoding="#{encoding}"?>|
end

defp format({:doctype, {:system, name, system}}, 0, _options),
do: ['<!DOCTYPE ', to_string(name), ' SYSTEM "', to_string(system), '">']
Expand Down
18 changes: 18 additions & 0 deletions test/xml_builder_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,24 @@ defmodule XmlBuilderTest do
expectation = "<level1>\n\t<level2>test_value</level2>\n</level1>"
assert XmlBuilder.generate(input(), whitespace: "\t") == expectation
end

test "encoding defaults to UTF-8" do
expectation = ~s|<?xml version="1.0" encoding="UTF-8"?>|
assert XmlBuilder.generate(:xml_decl) == expectation
end

test "encoding option is used" do
expectation = ~s|<?xml version="1.0" encoding="ISO-8859-1"?>|
assert XmlBuilder.generate(:xml_decl, encoding: "ISO-8859-1") == expectation
end

test "encoding option works with other options" do
xml = [XmlBuilder.element(:oldschool, [])]
|> XmlBuilder.document()
|> XmlBuilder.generate(format: :indent, encoding: "ISO-8859-1")
expectation = ~s|<?xml version="1.0" encoding="ISO-8859-1"?>\n<oldschool/>|
assert xml == expectation
end
end

test "element with content" do
Expand Down

0 comments on commit 88f1c7f

Please sign in to comment.