templates.ex (3225B)
1 defmodule ExDoc.Formatter.EPUB.Templates do 2 @moduledoc false 3 4 require EEx 5 6 alias ExDoc.Formatter.HTML 7 alias ExDoc.Formatter.HTML.Templates, as: H 8 9 @doc """ 10 Generate content from the module template for a given `node` 11 """ 12 def module_page(config, module_node) do 13 summary = H.module_summary(module_node) 14 module_template(config, module_node, summary) 15 end 16 17 @doc """ 18 Generated ID for static file 19 """ 20 def static_file_to_id(static_file) do 21 prefix = static_file |> HTML.filename_to_title() |> HTML.text_to_id() 22 extension = static_file |> Path.extname() |> String.replace_prefix(".", "-") 23 24 "#{prefix}#{extension}" 25 end 26 27 @doc """ 28 Creates the Package Document Definition. 29 30 this definition encapsulates the publication metadata and the resource 31 information that constitute the EPUB publication. This definition also 32 includes the default reading order. 33 34 See http://www.idpf.org/epub/30/spec/epub30-publications.html#sec-package-def. 35 """ 36 EEx.function_from_file( 37 :def, 38 :content_template, 39 Path.expand("templates/content_template.eex", __DIR__), 40 [:config, :nodes, :uuid, :datetime, :static_files], 41 trim: true 42 ) 43 44 @doc """ 45 Creates a chapter which contains all the details about an individual module. 46 47 This chapter can include the following sections: *functions*, *types*, *callbacks*. 48 """ 49 EEx.function_from_file( 50 :def, 51 :module_template, 52 Path.expand("templates/module_template.eex", __DIR__), 53 [:config, :module, :summary], 54 trim: true 55 ) 56 57 @doc """ 58 Creates the table of contents. 59 60 This template follows the EPUB Navigation Document Definition. 61 62 See http://www.idpf.org/epub/30/spec/epub30-contentdocs.html#sec-xhtml-nav. 63 """ 64 EEx.function_from_file( 65 :def, 66 :nav_template, 67 Path.expand("templates/nav_template.eex", __DIR__), 68 [:config, :nodes], 69 trim: true 70 ) 71 72 @doc """ 73 Creates a new chapter when the user provides additional files. 74 """ 75 EEx.function_from_file( 76 :def, 77 :extra_template, 78 Path.expand("templates/extra_template.eex", __DIR__), 79 [:config, :title, :title_content, :content], 80 trim: true 81 ) 82 83 @doc """ 84 Creates the cover page for the EPUB document. 85 """ 86 EEx.function_from_file( 87 :def, 88 :title_template, 89 Path.expand("templates/title_template.eex", __DIR__), 90 [:config], 91 trim: true 92 ) 93 94 EEx.function_from_file( 95 :defp, 96 :head_template, 97 Path.expand("templates/head_template.eex", __DIR__), 98 [:config, :page], 99 trim: true 100 ) 101 102 EEx.function_from_file( 103 :defp, 104 :nav_item_template, 105 Path.expand("templates/nav_item_template.eex", __DIR__), 106 [:name, :nodes], 107 trim: true 108 ) 109 110 EEx.function_from_file( 111 :defp, 112 :toc_item_template, 113 Path.expand("templates/toc_item_template.eex", __DIR__), 114 [:nodes], 115 trim: true 116 ) 117 118 "templates/media-types.txt" 119 |> Path.expand(__DIR__) 120 |> File.read!() 121 |> String.split("\n", trim: true) 122 |> Enum.each(fn line -> 123 [extension, media] = String.split(line, ",") 124 125 defp media_type("." <> unquote(extension)) do 126 unquote(media) 127 end 128 end) 129 130 defp media_type(arg), 131 do: raise("asset with extension #{inspect(arg)} is not supported by EPUB format") 132 end