Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix dialyzer issues in ChromicPDF.Template #294

Merged
merged 1 commit into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## Unreleased

### Fixed

- Explicitly pick keys in `ChromicPDF.Template` to fix Dialyzer "no local return" errors in clients.

## [1.15.0] - 2023-12-15

### Fixed
Expand Down
22 changes: 13 additions & 9 deletions lib/chromic_pdf/template.ex
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,17 @@ defmodule ChromicPDF.Template do

This example has the dimension of a ISO A4 page.

ChromicPDF.Template.source_and_options(
[
content: "<p>Hello</p>",
header: "<p>header</p>",
footer: "<p>footer</p>",
size: :a4,
header_height: "45mm",
header_font_size: "20pt",
footer_height: "40mm"
)
]
|> ChromicPDF.Template.source_and_options()
|> ChromicPDF.print_to_pdf()

Content, header, and footer templates should be unwrapped HTML markup (i.e. no `<html>` around
the content), including any `<style>` tags that your page needs.
Expand All @@ -161,12 +163,13 @@ defmodule ChromicPDF.Template do
@spec source_and_options([content_option() | header_footer_option() | style_option()]) ::
ChromicPDF.source_and_options()
def source_and_options(opts) do
styles = page_styles(opts)
content = Keyword.get(opts, :content, @default_content)
# Keep dialyzer happy by making sure we only pass on option keys as spec'd in lower funs.
{content, opts} = Keyword.pop(opts, :content, @default_content)
{header_and_footer_opts, style_opts} = Keyword.split(opts, [:header, :footer])

%{
source: {:html, html_concat(styles, content)},
opts: options(opts)
source: {:html, html_concat(page_styles(style_opts), content)},
opts: options(header_and_footer_opts ++ style_opts)
}
end

Expand Down Expand Up @@ -213,8 +216,8 @@ defmodule ChromicPDF.Template do
@spec options() :: keyword()
@spec options([header_footer_option() | style_option()]) :: keyword()
def options(opts \\ []) do
header = Keyword.get(opts, :header, "")
footer = Keyword.get(opts, :footer, "")
{header, opts} = Keyword.pop(opts, :header, "")
{footer, opts} = Keyword.pop(opts, :footer, "")
styles = header_footer_styles(opts)

[
Expand Down Expand Up @@ -311,7 +314,8 @@ defmodule ChromicPDF.Template do
swaps the page dimensions (e.g. it turns 11.7x8.3" A4 into 8.3"x11.7").
"""
@spec page_styles() :: binary()
@spec page_styles([style_option()]) :: binary()
# @spec page_styles([style_option()]) :: binary()
@spec page_styles(keyword) :: binary()
def page_styles(opts \\ []) do
opts
|> assigns_for_styles()
Expand Down