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

First approach of the exporter: mix prexent.export task #19

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@ npm-debug.log
.elixir_ls

*.iml
.idea
.idea

/exported/
55 changes: 55 additions & 0 deletions lib/mix/tasks/prexent_export.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
defmodule Mix.Tasks.Prexent.Export do
@moduledoc """
Run the Phoenix server with the endpoints defined by Prexent.

Execute `mix prexent` in your prexent project.

The default markdown file is `slides.md`, if you want to use another source file, run:

$ mix prexent FILE_NAME

The source markdown file is set in `Application.put_env(:prexent, :source_md, source_md)` to be consumed by the live view endpoint.

The default port is 4000, you can change it passing the `PORT` env:

$ PORT=4040 mix prexent

"""
use Mix.Task

@doc false
def run(args) do
source_md = get_source_name(args)
check_file!(source_md)
Mix.shell.info "Exporting to static presentation at 'exported/'.."
File.mkdir_p!("exported")
html = generate(source_md)
File.cd!("exported")
File.write!("index.html", html)
# copy static files
File.copy!("../priv/static/js/app.js", "app.js")
File.copy!("../priv/static/css/app.css", "app.css")
end

def generate(source_md) do
slides = Prexent.Parser.to_parsed_list(source_md)

assigns = %{
layout: {PrexentWeb.LayoutView, "export.html"},
slides: slides,
slide: 0,
code_runners: %{},
pid_slides: %{}
}

Phoenix.View.render_to_string(PrexentWeb.SlidesView, "slides.html", assigns)
end

defp get_source_name([]), do: "slides.md"
defp get_source_name(args), do: hd(args)

defp check_file!(filename) do
if not File.exists?(filename),
do: Mix.raise("The slides source file '#{filename}' does not exist")
end
end
17 changes: 17 additions & 0 deletions lib/prexent_web/templates/layout/export.html.eex
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Prexent</title>
<link href="https://fonts.googleapis.com/css?family=Open+Sans:600,700|Roboto|Roboto+Mono:400,400i&display=swap" rel="stylesheet">
<link rel="stylesheet" href="app.css"/>
</head>
<body>
<main>
<%= render @view_module, @view_template, assigns %>
</main>
<script type="text/javascript" src="app.js"></script>
</body>
</html>