pandocomatic 0.0.13 → 0.1.0.b

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/bin/pandocomatic +2 -78
  3. data/lib/pandocomatic/cli.rb +183 -0
  4. data/lib/pandocomatic/command/command.rb +113 -0
  5. data/lib/pandocomatic/command/convert_dir_command.rb +155 -0
  6. data/lib/pandocomatic/command/convert_file_command.rb +163 -0
  7. data/lib/pandocomatic/command/copy_file_command.rb +49 -0
  8. data/lib/pandocomatic/command/create_link_command.rb +85 -0
  9. data/lib/pandocomatic/command/skip_command.rb +50 -0
  10. data/lib/pandocomatic/configuration.rb +212 -164
  11. data/lib/pandocomatic/error/cli_error.rb +49 -0
  12. data/lib/pandocomatic/error/configuration_error.rb +39 -0
  13. data/lib/pandocomatic/error/io_error.rb +50 -0
  14. data/lib/pandocomatic/error/pandoc_error.rb +30 -0
  15. data/lib/pandocomatic/error/pandocomatic_error.rb +48 -0
  16. data/lib/pandocomatic/error/processor_error.rb +33 -0
  17. data/lib/pandocomatic/fileinfo_preprocessor.rb +34 -13
  18. data/lib/pandocomatic/pandoc_metadata.rb +66 -34
  19. data/lib/pandocomatic/pandocomatic.rb +176 -0
  20. data/lib/pandocomatic/printer/command_printer.rb +28 -0
  21. data/lib/pandocomatic/printer/configuration_errors_printer.rb +29 -0
  22. data/lib/pandocomatic/printer/error_printer.rb +34 -0
  23. data/lib/pandocomatic/printer/finish_printer.rb +44 -0
  24. data/lib/pandocomatic/printer/help_printer.rb +27 -0
  25. data/lib/pandocomatic/printer/printer.rb +43 -0
  26. data/lib/pandocomatic/printer/summary_printer.rb +35 -0
  27. data/lib/pandocomatic/printer/version_printer.rb +30 -0
  28. data/lib/pandocomatic/printer/views/cli_error.txt +12 -0
  29. data/lib/pandocomatic/printer/views/command.txt +1 -0
  30. data/lib/pandocomatic/printer/views/configuration_error.txt +1 -0
  31. data/lib/pandocomatic/printer/views/configuration_errors.txt +10 -0
  32. data/lib/pandocomatic/printer/views/error.txt +5 -0
  33. data/lib/pandocomatic/printer/views/finish.txt +1 -0
  34. data/lib/pandocomatic/printer/views/help.txt +135 -0
  35. data/lib/pandocomatic/printer/views/io_error.txt +12 -0
  36. data/lib/pandocomatic/printer/views/pandoc_error.txt +1 -0
  37. data/lib/pandocomatic/printer/views/processor_error.txt +6 -0
  38. data/lib/pandocomatic/printer/views/summary.txt +1 -0
  39. data/lib/pandocomatic/printer/views/version.txt +7 -0
  40. data/lib/pandocomatic/printer/views/warning.txt +1 -0
  41. data/lib/pandocomatic/printer/warning_printer.rb +33 -0
  42. data/lib/pandocomatic/processor.rb +25 -8
  43. data/lib/pandocomatic/warning.rb +36 -0
  44. metadata +79 -17
  45. data/lib/pandocomatic/dir_converter.rb +0 -119
  46. data/lib/pandocomatic/file_converter.rb +0 -99
@@ -1,119 +0,0 @@
1
- module Pandocomatic
2
-
3
- require 'fileutils'
4
- require_relative 'file_converter.rb'
5
-
6
- CONFIG_FILE = 'pandocomatic.yaml'
7
-
8
- class DirConverter
9
-
10
- def initialize src, dst, config
11
- @src_root = src
12
- @dst_root = dst
13
- @config = config
14
- end
15
-
16
- def convert src_dir = @src_root, dst_dir = @dst_root, config = @config
17
-
18
- ensure_directory dst_dir
19
- config = reconfigure config, src_dir
20
-
21
- # Convert each file and subdirectory according to the specifications set in config
22
- Dir.foreach src_dir do |filename|
23
- src = File.join src_dir, filename
24
-
25
- next if config.skip? src
26
-
27
- dst = File.join dst_dir, filename
28
-
29
- if File.symlink? src and not config.follow_links?
30
- # Symlinks are also recognized as files and directories, so first
31
- # check if they should be followed (and treated as files and
32
- # directories), or if they should be recreated (if follow-links
33
- # setting is false
34
- recreate_link src, dst
35
-
36
- elsif File.directory? src then
37
-
38
- if config.recursive? then
39
-
40
- # Convert subdirectories only when the recursivity is set in the
41
- # configuration.
42
- convert src, dst, config
43
- else
44
- next # skip directories when not recursive
45
- end
46
-
47
- elsif File.file? src
48
-
49
- raise "Cannot read file #{src}" if not File.readable? src
50
- raise "Cannot write file #{dst}" if File.exist? dst and not File.writable? dst
51
-
52
- # Check if the source file has to be converted. If so, convert;
53
- # otherwise, copy it to the destination tree
54
- if config.convert? src then
55
- dst = config.set_extension dst
56
- Pandocomatic::FileConverter.new.convert src, dst, config if file_modified? src, dst
57
- else
58
- # copy file
59
- FileUtils.cp src, dst if file_modified? src, dst
60
- end
61
-
62
- else
63
- warn "Unclear what to do with #{src}, skipping this file"
64
- next
65
- end
66
-
67
- end
68
-
69
- end
70
-
71
- private
72
-
73
- # If the source directory contains a configuration file, use it to
74
- # reconfigure the converter. Otherwise, use the current configuration
75
- def reconfigure current_config, src_dir
76
- config_file = File.join src_dir, CONFIG_FILE
77
- if File.exist? config_file then
78
- current_config.reconfigure config_file
79
- else
80
- current_config
81
- end
82
- end
83
-
84
- # Ensure that dir exist (and is a directory)
85
- def ensure_directory dir
86
- if Dir.exist? dir then
87
- raise "#{dir} is not a directory" if not File.directory? dir
88
- else
89
- begin
90
- Dir.mkdir dir
91
- rescue SystemCallError => e
92
- raise "Error trying to create directory #{dir}: #{e.message}"
93
- end
94
- end
95
- end
96
-
97
- # Recreate source link in destination tree if it points to somewhere inside
98
- # the source tree using relative paths
99
- def recreate_link src, dst
100
- src_target = File.readlink src
101
- if src_target.start_with? '.' then
102
- full_src_target = File.expand_path src_target, File.dirname(src)
103
- dst_target = src_target
104
- if full_src_target.start_with? File.absolute_path(@src_root)
105
- File.symlink dst_target, dst unless File.exist? dst
106
- else
107
- warn "Skipping link #{src} because it points to outside the source tree"
108
- end
109
- end
110
- end
111
-
112
- def file_modified? src, dst
113
- # src file is newer than the dstination file?
114
- not File.exist? dst or File.mtime(src) > File.mtime(dst)
115
- end
116
-
117
- end
118
-
119
- end
@@ -1,99 +0,0 @@
1
- module Pandocomatic
2
-
3
- require 'paru/pandoc'
4
- require_relative 'pandoc_metadata.rb'
5
- require_relative 'processor.rb'
6
- require_relative 'fileinfo_preprocessor'
7
-
8
- class FileConverter
9
-
10
- def convert src, dst, current_config
11
- @config = current_config
12
- @src = src
13
- metadata = PandocMetadata.load_file src
14
-
15
- if metadata.has_template? then
16
- template_name = metadata.template_name
17
- else
18
- template_name = @config.determine_template src
19
- end
20
-
21
- template = @config.get_template template_name
22
-
23
- pandoc_options = (template['pandoc'] || {}).merge(metadata.pandoc_options || {})
24
-
25
- input = File.read src
26
- input = FileInfoPreprocessor.run input, src
27
- input = preprocess input, template
28
- input = pandoc input, pandoc_options, File.dirname(src)
29
- output = postprocess input, template
30
-
31
- if dst.to_s.empty? and metadata.pandoc_options.has_key? 'output'
32
- dst = metadata.pandoc_options['output']
33
- end
34
-
35
- File.open( dst, 'w') do |file|
36
- file << output
37
- end
38
- end
39
-
40
- private
41
-
42
- PANDOC_OPTIONS_WITH_PATH = [
43
- 'filter',
44
- 'template',
45
- 'css',
46
- 'include-in-header',
47
- 'include-before-body',
48
- 'include-after-body',
49
- 'reference-odt',
50
- 'reference-docx',
51
- 'epub-stylesheet',
52
- 'epub-cover-image',
53
- 'epub-metadata',
54
- 'epub-embed-font',
55
- 'bibliography',
56
- 'csl'
57
- ]
58
-
59
- def pandoc input, options, src_dir
60
- converter = Paru::Pandoc.new
61
- options.each do |option, value|
62
-
63
- value = @config.update_path value, src_dir if
64
- PANDOC_OPTIONS_WITH_PATH.include? option
65
-
66
- converter.send option, value unless option == 'output'
67
- # don't let pandoc write the output to enable postprocessing
68
- end
69
-
70
- converter << input
71
- end
72
-
73
- def preprocess input, config
74
- process input, 'preprocessors', config
75
- end
76
-
77
- def postprocess input, config
78
- process input, 'postprocessors', config
79
- end
80
-
81
- # Run the input string through a list of filters called processors. There
82
- # are to types: preprocessors and postprocessors
83
- def process input, type, config
84
- if config.has_key? type then
85
- processors = config[type]
86
- output = input
87
- processors.each do |processor|
88
- script = @config.update_path(processor, File.dirname(@src))
89
- output = Processor.run(script, output)
90
- end
91
- output
92
- else
93
- input
94
- end
95
- end
96
-
97
- end
98
-
99
- end