|
| 1 | +require 'asciidoctor' |
| 2 | +require 'asciidoctor-pdf' |
| 3 | + |
1 | 4 | LANGS = %w[en fr].freeze
|
| 5 | +VERSIONS = %w[5 6].freeze |
| 6 | +OUTPUT_DIR = File.join __dir__, 'build' |
| 7 | +THEMES_DIR = File.join __dir__, 'themes' |
| 8 | +FONTS_DIR = File.join __dir__, 'fonts' |
2 | 9 |
|
3 |
| -def parse_lang(args) |
| 10 | +def check_args(args) |
4 | 11 | lang = args[:lang]
|
5 | 12 | unless LANGS.include?(lang)
|
6 | 13 | msg = format('Lang not availaible. Select on of theses langs: %s', LANGS.join(', '))
|
7 | 14 | raise ArgumentError, msg
|
8 | 15 | end
|
9 | 16 |
|
| 17 | + version = args[:version] |
| 18 | + unless VERSIONS.include?(version) |
| 19 | + msg = format('Version not availaible. Select on of theses versions: %s', VERSIONS.join(', ')) |
| 20 | + raise ArgumentError, msg |
| 21 | + end |
| 22 | + |
10 | 23 | lang
|
11 | 24 | end
|
12 | 25 |
|
13 |
| -def out_filename(lang, extension) |
14 |
| - "api_on_rails-#{lang}.#{extension}" |
| 26 | +def in_filename(args) |
| 27 | + format('rails%s/%s/api_on_rails.adoc', args[:version], args[:lang]) |
| 28 | +end |
| 29 | + |
| 30 | +def out_filename(args, extension) |
| 31 | + format("api_on_rails_%s-%s.%s",args[:version], args[:lang], extension) |
15 | 32 | end
|
16 | 33 |
|
17 | 34 | namespace :build do
|
18 | 35 | desc 'Build all versions'
|
19 |
| - task :all, [:lang] do |_task, args| |
20 |
| - lang = parse_lang(args) |
21 |
| - Rake::Task['build:pdf'].invoke(lang) |
22 |
| - Rake::Task['build:epub'].invoke(lang) |
23 |
| - Rake::Task['build:mobi'].invoke(lang) |
| 36 | + task :all, [:version, :lang] do |_task, args| |
| 37 | + check_args(args) |
| 38 | + Rake::Task['build:pdf'].invoke(args[:version], args[:lang]) |
| 39 | + Rake::Task['build:epub'].invoke(args[:version], args[:lang]) |
| 40 | + Rake::Task['build:mobi'].invoke(args[:version], args[:lang]) |
24 | 41 | end
|
25 | 42 |
|
26 | 43 | desc 'Build a PDF version'
|
27 |
| - task :pdf, [:lang] do |_task, args| |
28 |
| - lang = parse_lang(args) |
29 |
| - filename = out_filename lang, 'pdf' |
30 |
| - `asciidoctor-pdf #{lang}/api_on_rails.adoc --destination-dir build --out-file #{filename}` |
31 |
| - puts "Book compiled on build/#{filename}" |
| 44 | + task :pdf, [:version, :lang] do |_task, args| |
| 45 | + check_args(args) |
| 46 | + |
| 47 | + input = in_filename args |
| 48 | + output = out_filename args, 'pdf' |
| 49 | + |
| 50 | + Asciidoctor.convert_file input, |
| 51 | + safe: :unsafe, |
| 52 | + backend: 'pdf', |
| 53 | + to_dir: OUTPUT_DIR, |
| 54 | + mkdirs: true, |
| 55 | + to_file: output, |
| 56 | + attributes: { |
| 57 | + 'pdf-stylesdir' => THEMES_DIR, |
| 58 | + 'pdf-style' => 'my', |
| 59 | + 'pdf-fontsdir' => FONTS_DIR, |
| 60 | + } |
| 61 | + |
| 62 | + |
| 63 | + # `asciidoctor-pdf #{input} --destination-dir build --out-file #{output}` |
| 64 | + puts "Book compiled on build/#{output}" |
32 | 65 | end
|
33 | 66 |
|
34 | 67 | desc 'Build an HTML version'
|
35 |
| - task :html, [:lang] do |_task, args| |
36 |
| - lang = parse_lang(args) |
37 |
| - filename = out_filename lang, 'html' |
38 |
| - `asciidoctor #{lang}/api_on_rails.adoc --destination-dir build --out-file #{filename}` |
39 |
| - puts "Book compiled on build/#{filename}" |
| 68 | + task :html, [:version, :lang] do |_task, args| |
| 69 | + check_args(args) |
| 70 | + input = in_filename args |
| 71 | + output = out_filename args, 'html' |
| 72 | + `asciidoctor #{input} --destination-dir build --out-file #{output}` |
| 73 | + puts "Book compiled on build/#{output}" |
40 | 74 | end
|
41 | 75 |
|
42 | 76 | desc 'Build an EPUB version'
|
43 |
| - task :epub, [:lang] do |_task, args| |
44 |
| - lang = parse_lang(args) |
45 |
| - filename = out_filename lang, 'epub' |
46 |
| - `asciidoctor-epub3 #{lang}/api_on_rails.adoc --destination-dir build --out-file #{filename}` |
47 |
| - puts "Book compiled on build/#{filename}" |
| 77 | + task :epub, [:version, :lang] do |_task, args| |
| 78 | + check_args(args) |
| 79 | + input = in_filename args |
| 80 | + output = out_filename args, 'epub' |
| 81 | + `asciidoctor-epub3 #{input} --destination-dir build --out-file #{output}` |
| 82 | + puts "Book compiled on build/#{output}" |
48 | 83 | end
|
49 | 84 |
|
50 | 85 | desc 'Build a MOBI version'
|
51 |
| - task :mobi, [:lang] do |_task, args| |
52 |
| - lang = parse_lang(args) |
53 |
| - filename = out_filename lang, 'mobi' |
54 |
| - `asciidoctor-epub3 #{lang}/api_on_rails.adoc --destination-dir build -a ebook-format=kf8 --out-file #{filename}` |
55 |
| - `rm build/api_on_rails-#{lang}-kf8.epub` |
56 |
| - puts "Book compiled on build/api_on_rails-#{lang}.mobi" |
| 86 | + task :mobi, [:version, :lang] do |_task, args| |
| 87 | + check_args(args) |
| 88 | + input = in_filename args |
| 89 | + output = out_filename args, 'mobi' |
| 90 | + `asciidoctor-epub3 #{input} --destination-dir build -a ebook-format=kf8 --out-file #{output}` |
| 91 | + puts "Book compiled on build/#{output}" |
57 | 92 | end
|
58 | 93 | end
|
0 commit comments