muplin 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/Rakefile +1 -0
- data/bin/muplin +45 -0
- data/lib/muplin.rb +15 -0
- data/lib/muplin/outliner.rb +50 -0
- data/lib/muplin/version.rb +3 -0
- data/muplin.gemspec +24 -0
- metadata +67 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/muplin
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'optparse'
|
4
|
+
require 'rubygems'
|
5
|
+
require 'muplin'
|
6
|
+
|
7
|
+
|
8
|
+
Version = Muplin::VERSION
|
9
|
+
DEFAULT_OUTPUT = 'muplined.pdf'
|
10
|
+
|
11
|
+
|
12
|
+
def usage
|
13
|
+
<<"EOS"
|
14
|
+
Muplin is a MUriyari Pdf outLINer. https://github.com/pokutuna/muplin
|
15
|
+
Usage: muplin <source.pdf> <outline(.yaml|.json)> [options]
|
16
|
+
EOS
|
17
|
+
end
|
18
|
+
|
19
|
+
def load_outline(file)
|
20
|
+
ext = File.extname(file)
|
21
|
+
if ext == '.yaml'
|
22
|
+
require 'yaml'
|
23
|
+
YAML.load_file(file)
|
24
|
+
elsif ext == '.json'
|
25
|
+
require 'json'
|
26
|
+
JSON.parse(File.open(file).read)
|
27
|
+
else
|
28
|
+
puts usage
|
29
|
+
raise 'outline must be yaml or json'
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
OPTS = {}
|
35
|
+
OptionParser.new(usage) { |opt|
|
36
|
+
opt.on('-o', '--output=FILE', "write pdf to FILE. default: ./#{DEFAULT_OUTPUT}") { |v| OPTS[:o] = v}
|
37
|
+
opt.on('--overwrite', 'overwrite to <source.pdf>') { |v| OPTS[:overwrite] = v}
|
38
|
+
opt.parse!(ARGV)
|
39
|
+
}
|
40
|
+
|
41
|
+
puts usage if ARGV.size < 2
|
42
|
+
src_file, outline_file = ARGV
|
43
|
+
|
44
|
+
OPTS[:o] = src_file if OPTS[:overwrite]
|
45
|
+
Muplin.exec(src_file, load_outline(outline_file), OPTS[:o] || DEFAULT_OUTPUT)
|
data/lib/muplin.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'prawn'
|
2
|
+
require "muplin/version"
|
3
|
+
require 'muplin/outliner'
|
4
|
+
|
5
|
+
module Muplin
|
6
|
+
|
7
|
+
def self.exec(src_file, outline, dest_file)
|
8
|
+
doc = Muplin::Outliner.new(src_file)
|
9
|
+
doc.clear_outlines
|
10
|
+
raise ArgumentError 'invalid outline' unless doc.instance_eval { sections? outline }
|
11
|
+
doc.define_outline(outline)
|
12
|
+
doc.render_file(dest_file)
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
class Muplin::Outliner
|
2
|
+
|
3
|
+
def initialize(filename)
|
4
|
+
@pdf = Prawn::Document.new(:template => filename)
|
5
|
+
end
|
6
|
+
|
7
|
+
|
8
|
+
public
|
9
|
+
def clear_outlines
|
10
|
+
@pdf.state.store.root.data[:Outlines] = nil
|
11
|
+
end
|
12
|
+
|
13
|
+
def define_outline(outlines, parent = nil)
|
14
|
+
outlines.each do |item|
|
15
|
+
if a_section?(item)
|
16
|
+
add_section(item, parent)
|
17
|
+
@last_label = item['label']
|
18
|
+
elsif sections?(item)
|
19
|
+
define_outline(item, @last_label)
|
20
|
+
else
|
21
|
+
raise ArgumentError, item.inspect
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def render_file(filename)
|
27
|
+
@pdf.render_file(filename)
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
private
|
32
|
+
def a_section?(item)
|
33
|
+
Hash === item and item.has_key?('label') and item.has_key?('page')
|
34
|
+
end
|
35
|
+
|
36
|
+
def sections?(item)
|
37
|
+
Array === item
|
38
|
+
end
|
39
|
+
|
40
|
+
def add_section(section, parent)
|
41
|
+
if parent.nil?
|
42
|
+
@pdf.outline.section section['label'], :destination => section['page']
|
43
|
+
else
|
44
|
+
@pdf.outline.add_subsection_to(parent) {
|
45
|
+
@pdf.outline.page :title => section['label'], :destination => section['page']
|
46
|
+
}
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
data/muplin.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "muplin/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "muplin"
|
7
|
+
s.version = Muplin::VERSION
|
8
|
+
s.authors = ["pokutuna"]
|
9
|
+
s.email = ["popopopopokutuna@gmail.com"]
|
10
|
+
s.homepage = "https://github.com/pokutuna/muplin"
|
11
|
+
s.summary = %q{A pure-ruby application to add (toc|outline) to a PDF file irresponsibly}
|
12
|
+
s.description = %q{Muplin is abbrev of MUriyari(irresponsibly force) Pdf outLINer. Muplin adds outlines that consist from pairs of label and page number to a PDF file. see also https://github.com/pokutuna/muplin}
|
13
|
+
|
14
|
+
s.rubyforge_project = "muplin"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
# specify any dependencies here; for example:
|
22
|
+
# s.add_development_dependency "rspec"
|
23
|
+
s.add_runtime_dependency(%q<prawn>, ["~> 0.12.0"])
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: muplin
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- pokutuna
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-10-19 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: prawn
|
16
|
+
requirement: &70230386848600 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 0.12.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70230386848600
|
25
|
+
description: Muplin is abbrev of MUriyari(irresponsibly force) Pdf outLINer. Muplin
|
26
|
+
adds outlines that consist from pairs of label and page number to a PDF file. see
|
27
|
+
also https://github.com/pokutuna/muplin
|
28
|
+
email:
|
29
|
+
- popopopopokutuna@gmail.com
|
30
|
+
executables:
|
31
|
+
- muplin
|
32
|
+
extensions: []
|
33
|
+
extra_rdoc_files: []
|
34
|
+
files:
|
35
|
+
- .gitignore
|
36
|
+
- Gemfile
|
37
|
+
- Rakefile
|
38
|
+
- bin/muplin
|
39
|
+
- lib/muplin.rb
|
40
|
+
- lib/muplin/outliner.rb
|
41
|
+
- lib/muplin/version.rb
|
42
|
+
- muplin.gemspec
|
43
|
+
homepage: https://github.com/pokutuna/muplin
|
44
|
+
licenses: []
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options: []
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ! '>='
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
requirements: []
|
62
|
+
rubyforge_project: muplin
|
63
|
+
rubygems_version: 1.8.10
|
64
|
+
signing_key:
|
65
|
+
specification_version: 3
|
66
|
+
summary: A pure-ruby application to add (toc|outline) to a PDF file irresponsibly
|
67
|
+
test_files: []
|