Skip to content

Commit

Permalink
Code formatting using Elixir 1.6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ricn committed Jan 18, 2018
1 parent c317c02 commit 528c6a5
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 59 deletions.
3 changes: 3 additions & 0 deletions .formatter.exs
@@ -0,0 +1,3 @@
[
inputs: ["mix.exs", "{config,lib,test}/**/*.{ex,exs}"]
]
62 changes: 37 additions & 25 deletions lib/librex.ex
Expand Up @@ -2,41 +2,40 @@ defmodule Librex do
@moduledoc """
Provides functions to convert office documents, spreadsheets & presentations to other formats.
LibreOffice must be installed. It's recommended that you add the soffice binary your PATH. Otherwise you have to specify the
absolute path to the soffice binary as the last parameter.
LibreOffice must be installed. It's recommended that you add the soffice binary your PATH. Otherwise you have to specify the absolute path to the soffice binary as the last parameter.
## Examples
## Examples
iex(1)> Librex.convert("test/fixtures/docx.docx", "/Users/ricn/docx.pdf")
iex(1)> Librex.convert("test/fixtures/docx.docx", "/Users/ricn/docx.pdf")
`{:ok, "/Users/ricn/docx.pdf"}`
`{:ok, "/Users/ricn/docx.pdf"}`
iex(2)> Librex.convert("non_existent_file", "/Users/ricn/docx.pdf")
iex(2)> Librex.convert("non_existent_file", "/Users/ricn/docx.pdf")
`{:error, :enoent}`
`{:error, :enoent}`
iex(3)> Librex.convert!("test/fixtures/docx.docx", "/Users/ricn/docx.pdf")
iex(3)> Librex.convert!("test/fixtures/docx.docx", "/Users/ricn/docx.pdf")
"/Users/ricn/docx.pdf"
"/Users/ricn/docx.pdf"
iex(4)> Librex.convert!("non_existent_file", "/Users/ricn/docx.pdf")
iex(4)> Librex.convert!("non_existent_file", "/Users/ricn/docx.pdf")
** (File.Error) could not read non_existent_file: no such file or directory (librex) lib/librex.ex:13: Librex.convert!/3
** (File.Error) could not read non_existent_file: no such file or directory (librex) lib/librex.ex:13: Librex.convert!/3
iex(5)> Librex.convert("test/fixtures/docx.docx", "/Users/ricn/docx.pdf", "/path_to/soffice")
iex(5)> Librex.convert("test/fixtures/docx.docx", "/Users/ricn/docx.pdf", "/path_to/soffice")
`{:ok, "/Users/ricn/docx.pdf"}`
`{:ok, "/Users/ricn/docx.pdf"}`
"""
"""

@doc """
Converts in_file to out_file
Returns `:ok` if successful, `{:error, reason}` otherwise.
"""
def convert(in_file, out_file, soffice_cmd \\ "soffice") do
case validate(in_file, out_file, soffice_cmd) do
{:ok, _} -> do_convert(in_file, out_file, soffice_cmd)
{:error, reason } -> {:error, reason}
{:ok, _} -> do_convert(in_file, out_file, soffice_cmd)
{:error, reason} -> {:error, reason}
end
end

Expand All @@ -45,7 +44,7 @@ defmodule Librex do
"""
def convert!(in_file, out_file, soffice_cmd \\ "soffice") do
case convert(in_file, out_file, soffice_cmd) do
{:ok, _} -> out_file
{:ok, _} -> out_file
{:error, reason} -> raise_error(reason, in_file)
end
end
Expand Down Expand Up @@ -91,15 +90,17 @@ defmodule Librex do

defp validate_output(result, output_file) do
output_ext = String.replace(Path.extname(output_file), ".", "")
if Enum.any?(supported_formats(), fn(ext) -> ext == output_ext end) do

if Enum.any?(supported_formats(), fn ext -> ext == output_ext end) do
result
else
{:error, "#{output_ext} is not a supported output format"}
end
end

defp supported_formats do
supported_document_formats() ++ supported_presentation_formats() ++ supported_spreadsheet_formats()
supported_document_formats() ++
supported_presentation_formats() ++ supported_spreadsheet_formats()
end

defp validate_soffice(result, soffice_cmd) do
Expand All @@ -113,21 +114,32 @@ defmodule Librex do
defp do_convert(in_file, out_file, soffice_cmd) do
basename = Path.basename(in_file, Path.extname(in_file))
convert_to = String.replace(Path.extname(out_file), ".", "")
out_temp_dir = System.tmp_dir! <> "/" <> SecureRandom.uuid <> "/"
out_temp_dir = System.tmp_dir!() <> "/" <> SecureRandom.uuid() <> "/"
out_temp_file = out_temp_dir <> basename <> Path.extname(out_file)

run(Path.expand(in_file), out_temp_dir, convert_to, soffice_cmd)

File.cp! out_temp_file, Path.expand(out_file)
File.rm! out_temp_file
File.rmdir! out_temp_dir
File.cp!(out_temp_file, Path.expand(out_file))
File.rm!(out_temp_file)
File.rmdir!(out_temp_dir)

{:ok, out_file}
end

defp run(file_to_convert, out_dir, convert_to, soffice_cmd) do
user_installation = "-env:UserInstallation=file://" <> System.tmp_dir! <> "/" <> "librex_oouser"
opts = [user_installation, "--headless", "--convert-to", convert_to, "--outdir", out_dir, file_to_convert]
user_installation =
"-env:UserInstallation=file://" <> System.tmp_dir!() <> "/" <> "librex_oouser"

opts = [
user_installation,
"--headless",
"--convert-to",
convert_to,
"--outdir",
out_dir,
file_to_convert
]

cmd = System.find_executable(soffice_cmd)
System.cmd(cmd, opts, stderr_to_stdout: true)
end
Expand Down
40 changes: 23 additions & 17 deletions mix.exs
Expand Up @@ -2,36 +2,42 @@ defmodule Librex.Mixfile do
use Mix.Project

def project do
[app: :librex,
version: "1.0.2",
elixir: "~> 1.0",
description: description(),
package: package(),
deps: deps(),
test_coverage: [tool: ExCoveralls]]
[
app: :librex,
version: "1.0.2",
elixir: "~> 1.0",
description: description(),
package: package(),
deps: deps(),
test_coverage: [tool: ExCoveralls]
]
end

def application do
[applications: [:logger]]
end

defp deps do
[{:secure_random, "~> 0.5"},
{:inch_ex, "~> 0.5", only: :docs},
{:earmark, "~> 1.2", only: :dev},
{:ex_doc, "~> 0.16", only: :dev},
{:excoveralls, "~> 0.6", only: [:dev, :test]}]
[
{:secure_random, "~> 0.5"},
{:inch_ex, "~> 0.5", only: :docs},
{:earmark, "~> 1.2", only: :dev},
{:ex_doc, "~> 0.16", only: :dev},
{:excoveralls, "~> 0.6", only: [:dev, :test]}
]
end

defp description do
"Convert office documents to other formats using LibreOffice"
end

defp package do
[files: ["lib", "mix.exs", "README*", "LICENSE*"],
contributors: ["Richard Nyström", "Sergey Chechaev"],
maintainers: ["Richard Nyström"],
licenses: ["MIT"],
links: %{"GitHub" => "https://github.com/ricn/librex", "Docs" => "http://hexdocs.pm/librex"}]
[
files: ["lib", "mix.exs", "README*", "LICENSE*"],
contributors: ["Richard Nyström", "Sergey Chechaev"],
maintainers: ["Richard Nyström"],
licenses: ["MIT"],
links: %{"GitHub" => "https://github.com/ricn/librex", "Docs" => "http://hexdocs.pm/librex"}
]
end
end
42 changes: 25 additions & 17 deletions test/librex_test.exs
Expand Up @@ -13,37 +13,37 @@ defmodule LibrexTest do
@non_existent_file Path.join(__DIR__, "fixtures/non.existent")

test ".convert docx to other supported formats" do
Enum.each(["pdf", "odt"], fn(format) -> test_conversion(@docx_file, format) end)
Enum.each(["pdf", "odt"], fn format -> test_conversion(@docx_file, format) end)
end

test ".convert odt to other supported formats" do
Enum.each(["pdf", "docx"], fn(format) -> test_conversion(@odt_file, format) end)
Enum.each(["pdf", "docx"], fn format -> test_conversion(@odt_file, format) end)
end

test ".convert pptx to other supported formats" do
Enum.each(["pdf", "odp"], fn(format) -> test_conversion(@pptx_file, format) end)
Enum.each(["pdf", "odp"], fn format -> test_conversion(@pptx_file, format) end)
end

test ".convert odp to other supported formats" do
Enum.each(["pdf", "pptx"], fn(format) -> test_conversion(@odp_file, format) end)
Enum.each(["pdf", "pptx"], fn format -> test_conversion(@odp_file, format) end)
end

test ".convert xlsx to other supported formats" do
Enum.each(["pdf", "ods"], fn(format) -> test_conversion(@xlsx_file, format) end)
Enum.each(["pdf", "ods"], fn format -> test_conversion(@xlsx_file, format) end)
end

test ".convert ods to other supported formats" do
Enum.each(["pdf", "xlsx"], fn(format) -> test_conversion(@ods_file, format) end)
Enum.each(["pdf", "xlsx"], fn format -> test_conversion(@ods_file, format) end)
end

defp test_conversion(input_file, output_format) do
output_file = random_path() <> "." <> output_format
refute File.exists? output_file
assert { :ok, output_file } == Librex.convert(input_file, output_file)
refute File.exists?(output_file)
assert {:ok, output_file} == Librex.convert(input_file, output_file)
end

test ".convert must return error when file to convert does not exist" do
assert { :error, :enoent } == Librex.convert(@non_existent_file, "/tmp/output.pdf")
assert {:error, :enoent} == Librex.convert(@non_existent_file, "/tmp/output.pdf")
end

test ".convert! must return output file path" do
Expand All @@ -53,55 +53,63 @@ defmodule LibrexTest do

test ".convert! must raise error when file to convert does not exist" do
msg = "could not read \"#{@non_existent_file}\": no such file or directory"

assert_raise File.Error, msg, fn ->
Librex.convert!(@non_existent_file, "/tmp/output.pdf")
end
end

test "convert must return error when LibreOffice executable can't be found" do
cmd = "sofice" # misspelled
# misspelled
cmd = "sofice"
msg = "LibreOffice (#{cmd}) executable could not be found."
assert {:error, msg} == Librex.convert(@docx_file, "/tmp/output.pdf", "sofice")
end

test "convert! must raise error when LibreOffice executable can't be found" do
cmd = "sofice" # misspelled
# misspelled
cmd = "sofice"
msg = "LibreOffice (#{cmd}) executable could not be found."

assert_raise RuntimeError, msg, fn ->
Librex.convert!(@docx_file, "/tmp/output.pdf", "sofice")
end
end

test ".convert must have the possibility to specify LibreOffice command" do
pdf_file = random_path() <> ".pdf"
assert { :ok, pdf_file } == Librex.convert(@docx_file, pdf_file, System.find_executable("soffice"))

assert {:ok, pdf_file} ==
Librex.convert(@docx_file, pdf_file, System.find_executable("soffice"))
end

test ".convert must return error when file to convert is directory" do
pdf_file = random_path() <> ".pdf"
assert Librex.convert(System.tmp_dir!, pdf_file) == {:error, :eisdir}
assert Librex.convert(System.tmp_dir!(), pdf_file) == {:error, :eisdir}
end

test ".convert! must raise error when file to convert is directory" do
msg = "could not read \"#{System.tmp_dir!}\": illegal operation on a directory"
msg = "could not read \"#{System.tmp_dir!()}\": illegal operation on a directory"

assert_raise File.Error, msg, fn ->
Librex.convert!(System.tmp_dir!, "/tmp/output.pdf")
Librex.convert!(System.tmp_dir!(), "/tmp/output.pdf")
end
end

test ".convert must return error when output file has wrong extension" do
{ :error, reason } = Librex.convert(@docx_file, "/tmp/output.mp3")
{:error, reason} = Librex.convert(@docx_file, "/tmp/output.mp3")
assert reason == "mp3 is not a supported output format"
end

test "convert! must raise error when output file has wrong extension" do
msg = "mp3 is not a supported output format"

assert_raise RuntimeError, msg, fn ->
Librex.convert!(@docx_file, "/tmp/output.mp3")
end
end

defp random_path do
System.tmp_dir! <> "/" <> SecureRandom.uuid
System.tmp_dir!() <> "/" <> SecureRandom.uuid()
end
end

0 comments on commit 528c6a5

Please sign in to comment.