zenml-book 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a5ec10455d0b63aa7ee5f2ea3a1ab6ad0c8d8350cb58d8b7d54d8183c259ee51
4
+ data.tar.gz: 8bf8674adfbe08d5297f131605195edb3895157bd8d361bf0405fe31f611f39c
5
+ SHA512:
6
+ metadata.gz: f1b8330e967c1695502760e950ab311b15c0505e71fdb0a5052bc03f97429d8ac5c9ef03e9ea02c3900db36e7e0ee937ac1eafc9bbdc2cc27b768a8ccb3f6b11
7
+ data.tar.gz: cf6ae96d73573ee0d7c57813746deae07fa191f31b12d12c2df916c824e3feec0372f55e1459d085210308f08215f1f4e0f0445c2299cb80bfe801790d211e25
@@ -0,0 +1,134 @@
1
+ # coding: utf-8
2
+
3
+
4
+ require 'fileutils'
5
+ require 'open3'
6
+ require 'pp'
7
+ require 'rexml/document'
8
+ include REXML
9
+
10
+
11
+ class WholeBookConverter
12
+
13
+ TYPESET_COMMAND = "cd %O & AHFCmd -pgbar -x 3 -d main.fo -p @PDF -o document.pdf 2> error.txt"
14
+ OPEN_COMMANDS = {
15
+ :sumatra => "SumatraPDF -reuse-instance %O/document.pdf",
16
+ :formatter => "AHFormatter -s -d %O/main.fo"
17
+ }
18
+
19
+ def initialize(args)
20
+ @typeset, @open_type = false, nil
21
+ @dirs = {:output => "out", :document => "document", :template => "template"}
22
+ options, rest_args = args.partition{|s| s =~ /^\-\w+$/}
23
+ if options.include?("-t")
24
+ @typeset = true
25
+ end
26
+ if options.include?("-os")
27
+ @open_type = :sumatra
28
+ elsif options.include?("-of")
29
+ @open_type = :formatter
30
+ end
31
+ @rest_args = rest_args
32
+ end
33
+
34
+ def execute
35
+ path = File.join(@dirs[:document], "main.zml")
36
+ convert_normal(path)
37
+ convert_typeset(path) if @typeset
38
+ convert_open(path) if @open_type
39
+ end
40
+
41
+ def convert_normal(path)
42
+ output_path = path.gsub(@dirs[:document], @dirs[:output]).gsub(".zml", ".fo")
43
+ parser = create_parser.tap{|s| s.update(File.read(path))}
44
+ converter = create_converter.tap{|s| s.update(parser.run)}
45
+ formatter = create_formatter
46
+ File.open(output_path, "w") do |file|
47
+ puts("")
48
+ print_progress("Convert")
49
+ formatter.write(converter.convert, file)
50
+ end
51
+ end
52
+
53
+ def convert_typeset(path)
54
+ progress = {:format => 0, :render => 0}
55
+ command = TYPESET_COMMAND.gsub("%O", @dirs[:output])
56
+ stdin, stdout, stderr, thread = Open3.popen3(command)
57
+ stdin.close
58
+ stdout.each_char do |char|
59
+ if char == "." || char == "-"
60
+ type = (char == ".") ? :format : :render
61
+ progress[type] += 1
62
+ print_progress("Typeset", progress)
63
+ end
64
+ end
65
+ thread.join
66
+ end
67
+
68
+ def convert_open(path)
69
+ command = OPEN_COMMANDS[@open_type].gsub("%O", @dirs[:output])
70
+ stdin, stdout, stderr, thread = Open3.popen3(command)
71
+ stdin.close
72
+ end
73
+
74
+ def print_progress(type, progress = nil)
75
+ output = ""
76
+ output << "\e[1A\e[K"
77
+ output << "\e[0m\e[4m"
78
+ output << type
79
+ output << "\e[0m : \e[36m"
80
+ output << "%3d" % (progress&.fetch(:format, 0) || 0)
81
+ output << "\e[0m + \e[35m"
82
+ output << "%3d" % (progress&.fetch(:render, 0) || 0)
83
+ output << "\e[0m"
84
+ puts(output)
85
+ end
86
+
87
+ def create_parser(main = true)
88
+ parser = ZenithalParser.new("")
89
+ parser.brace_name = "x"
90
+ parser.bracket_name = "xn"
91
+ parser.slash_name = "i"
92
+ if main
93
+ parser.register_macro("import") do |attributes, _|
94
+ import_path = attributes["src"]
95
+ import_parser = create_parser(false)
96
+ import_parser.update(File.read(File.join(@dirs[:document], import_path)))
97
+ document = import_parser.run
98
+ import_nodes = (attributes["expand"]) ? document.root.children : [document.root]
99
+ next import_nodes
100
+ end
101
+ end
102
+ return parser
103
+ end
104
+
105
+ def create_converter
106
+ converter = ZenithalConverter.new(nil)
107
+ Dir.each_child(@dirs[:template]) do |entry|
108
+ if entry.end_with?(".rb")
109
+ binding = TOPLEVEL_BINDING
110
+ binding.local_variable_set(:converter, converter)
111
+ Kernel.eval(File.read(File.join(@dirs[:template], entry)), binding, entry)
112
+ end
113
+ end
114
+ return converter
115
+ end
116
+
117
+ def create_formatter
118
+ formatter = Formatters::Default.new
119
+ return formatter
120
+ end
121
+
122
+ def output_dir=(dir)
123
+ @dirs[:output] = dir
124
+ end
125
+
126
+ def document_dir=(dir)
127
+ @dirs[:document] = dir
128
+ end
129
+
130
+ def template_dir=(dir)
131
+ @dirs[:template] = dir
132
+ end
133
+
134
+ end
@@ -0,0 +1,10 @@
1
+ # coding: utf-8
2
+
3
+
4
+ module Zenithal::Book
5
+
6
+ VERSION = "1.0.0"
7
+
8
+ require_relative 'book/converter'
9
+
10
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zenml-book
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Ziphil
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-02-18 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: 'To be written.
14
+
15
+ '
16
+ email:
17
+ - ziphil.shaleiras@gmail.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - source/zenml/book.rb
23
+ - source/zenml/book/converter.rb
24
+ homepage: https://github.com/Ziphil/ZenithalBook
25
+ licenses:
26
+ - MIT
27
+ metadata: {}
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - source
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: '2.5'
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubygems_version: 3.0.3
44
+ signing_key:
45
+ specification_version: 4
46
+ summary: Summary
47
+ test_files: []