fyodor 0.2.10 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +19 -1
- data/lib/fyodor/book.rb +2 -2
- data/lib/fyodor/config_getter.rb +40 -16
- data/lib/fyodor/output_generator.rb +93 -0
- data/lib/fyodor/output_writer.rb +6 -4
- data/lib/fyodor/version.rb +1 -1
- metadata +3 -3
- data/lib/fyodor/md_generator.rb +0 -96
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 928a21f90c52229824428fa62a99e0ccd1b3319c954aa9f89696833933920315
|
4
|
+
data.tar.gz: ceedbb0b474de8f63f72fb4388f1e4eaf2a540644a61e26ed5a7616c0def2dbc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 44a7b6488f71988cdbfdb0b35083ae3e156e66899eee560188fc11da7127ff190edc60e8d9f22b3fab97cc96462efdc7732dbf905ce22b6cf87af14f5b41e61e
|
7
|
+
data.tar.gz: 53e882dd8ea580dd5657d088b064a00b45f86b145b8fbf403c0c84bbd9de5d7549933345b8291a7a2bc73260d5a3a5e98f711fd7b235953a2f554f394022f89d
|
data/README.md
CHANGED
@@ -78,6 +78,24 @@ time = "Adicionado:"
|
|
78
78
|
|
79
79
|
In the configuration file you can also set whether to print the time of each entry. On `[output]`, set `time` to `true` or `false`.
|
80
80
|
|
81
|
+
### Templating
|
82
|
+
|
83
|
+
If you want to change the structure of the files output by Fyodor, you can use custom templates.
|
84
|
+
|
85
|
+
It should be a ERB template and placed at `~/.config/fyodor/template.erb` or `$XDG_CONFIG_HOME/fyodor/template.erb`. Fyodor will find and use it automatically.
|
86
|
+
|
87
|
+
You can find a sample close to the default [here](docs/template.erb.sample). You can use any method or attribute available [at this class](lib/fyodor/output_generator.rb).
|
88
|
+
|
89
|
+
|
90
|
+
### Extension
|
91
|
+
|
92
|
+
If you want to change the extension of the output files - typically after changing the template -, set `extension` on fyodor.toml. For example, to change it to HTML:
|
93
|
+
|
94
|
+
```toml
|
95
|
+
[output]
|
96
|
+
extension = "html"
|
97
|
+
```
|
98
|
+
|
81
99
|
## Usage
|
82
100
|
|
83
101
|
```
|
@@ -105,4 +123,4 @@ If you like Fyodor, you can show your support here:
|
|
105
123
|
|
106
124
|
Licensed under [GPLv3](LICENSE)
|
107
125
|
|
108
|
-
Copyright (C) 2019-
|
126
|
+
Copyright (C) 2019-2022 [Rafael Cavalcanti](https://rafaelc.org/dev)
|
data/lib/fyodor/book.rb
CHANGED
@@ -26,8 +26,8 @@ module Fyodor
|
|
26
26
|
end
|
27
27
|
|
28
28
|
def basename
|
29
|
-
base = @author.to_s.empty? ? @title : "#{@author} - #{@title}"
|
30
|
-
base.strip
|
29
|
+
base = @author.to_s.empty? ? "Author N/A - #{@title}" : "#{@author} - #{@title}"
|
30
|
+
base.strip
|
31
31
|
end
|
32
32
|
|
33
33
|
def count_types
|
data/lib/fyodor/config_getter.rb
CHANGED
@@ -4,7 +4,10 @@ require "toml"
|
|
4
4
|
|
5
5
|
module Fyodor
|
6
6
|
class ConfigGetter
|
7
|
-
|
7
|
+
CONFIG_NAME = "fyodor.toml"
|
8
|
+
TEMPLATE_NAME = "template.erb"
|
9
|
+
|
10
|
+
DEFAULTS = {
|
8
11
|
"parser" => {
|
9
12
|
"highlight" => "Your Highlight",
|
10
13
|
"note" => "Your Note",
|
@@ -15,7 +18,8 @@ module Fyodor
|
|
15
18
|
"time" => "Added on"
|
16
19
|
},
|
17
20
|
"output" => {
|
18
|
-
"time" => false
|
21
|
+
"time" => false,
|
22
|
+
"extension" => "md"
|
19
23
|
}
|
20
24
|
}
|
21
25
|
|
@@ -28,29 +32,49 @@ module Fyodor
|
|
28
32
|
|
29
33
|
def get_config
|
30
34
|
Hash.include CoreExtensions::Hash::Merging
|
31
|
-
print_path
|
32
35
|
|
33
|
-
|
34
|
-
|
36
|
+
@config_path = get_path(CONFIG_NAME)
|
37
|
+
print_config_path
|
38
|
+
user_config = @config_path.nil? ? {} : TOML.load_file(@config_path)
|
39
|
+
|
40
|
+
config = DEFAULTS.deep_merge(user_config)
|
41
|
+
@template_path = get_path(TEMPLATE_NAME)
|
42
|
+
config["output"]["template_path"] = @template_path
|
43
|
+
print_template_path
|
44
|
+
|
45
|
+
config
|
35
46
|
end
|
36
47
|
|
37
|
-
def
|
38
|
-
|
48
|
+
def get_path(name)
|
49
|
+
possible_dirs.each do |d|
|
50
|
+
path = d + name
|
51
|
+
return path if path.exist?
|
52
|
+
end
|
53
|
+
|
54
|
+
return nil
|
39
55
|
end
|
40
56
|
|
41
|
-
def
|
42
|
-
return @
|
57
|
+
def possible_dirs
|
58
|
+
return @possible_dirs unless @possible_dirs.nil?
|
43
59
|
|
44
|
-
@
|
45
|
-
@
|
46
|
-
@
|
60
|
+
@possible_dirs = []
|
61
|
+
@possible_dirs << Pathname.new(ENV["XDG_CONFIG_HOME"]) + "fyodor" unless ENV["XDG_CONFIG_HOME"].nil?
|
62
|
+
@possible_dirs << Pathname.new("~/.config/fyodor").expand_path
|
63
|
+
end
|
64
|
+
|
65
|
+
def print_config_path
|
66
|
+
if @config_path.nil?
|
67
|
+
puts "No config found: using defaults.\n"
|
68
|
+
else
|
69
|
+
puts "Using config at #{@config_path}.\n"
|
70
|
+
end
|
47
71
|
end
|
48
72
|
|
49
|
-
def
|
50
|
-
if
|
51
|
-
puts "No
|
73
|
+
def print_template_path
|
74
|
+
if @template_path.nil?
|
75
|
+
puts "No template found: using default.\n\n"
|
52
76
|
else
|
53
|
-
puts "Using
|
77
|
+
puts "Using template at #{@template_path}.\n\n"
|
54
78
|
end
|
55
79
|
end
|
56
80
|
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
require "fyodor/strings"
|
2
|
+
require "erb"
|
3
|
+
|
4
|
+
module Fyodor
|
5
|
+
class OutputGenerator
|
6
|
+
include Strings
|
7
|
+
|
8
|
+
# The use of <% - 1 %> is a workaround for trimming indentation before <%=.
|
9
|
+
DEFAULT_TEMPLATE = %q{<% -%>
|
10
|
+
<%- 1 %><%= "# #{@book.basename}" %>
|
11
|
+
<% if regular_entries.size > 0 %>
|
12
|
+
<%- 1 %><%= "## Highlights and notes" %>
|
13
|
+
|
14
|
+
<%- 1 %><%= render_entries(regular_entries) %>
|
15
|
+
<% end -%>
|
16
|
+
<% if bookmarks.size > 0 %>
|
17
|
+
<%- 1 %><%= "## Bookmarks" %>
|
18
|
+
|
19
|
+
<%- 1 %><%= render_entries(bookmarks) %>
|
20
|
+
<% end -%>
|
21
|
+
}
|
22
|
+
|
23
|
+
def initialize(book, config)
|
24
|
+
@book = book
|
25
|
+
@config = config
|
26
|
+
end
|
27
|
+
|
28
|
+
def content
|
29
|
+
ERB.new(template, nil, '-').result(binding)
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def template
|
36
|
+
return DEFAULT_TEMPLATE if @config["template_path"].to_s.empty?
|
37
|
+
|
38
|
+
File.read(@config["template_path"])
|
39
|
+
end
|
40
|
+
|
41
|
+
def regular_entries
|
42
|
+
@book.reject { |entry| entry.type == Entry::TYPE[:bookmark] }
|
43
|
+
end
|
44
|
+
|
45
|
+
def bookmarks
|
46
|
+
@book.select { |entry| entry.type == Entry::TYPE[:bookmark] }
|
47
|
+
end
|
48
|
+
|
49
|
+
def render_entries(entries)
|
50
|
+
output = ""
|
51
|
+
entries.each do |entry|
|
52
|
+
output += "- #{item_text(entry)}\n\n"
|
53
|
+
output += " #{item_desc(entry)}\n\n"
|
54
|
+
end
|
55
|
+
output
|
56
|
+
end
|
57
|
+
|
58
|
+
def item_text(entry)
|
59
|
+
case entry.type
|
60
|
+
when Entry::TYPE[:bookmark]
|
61
|
+
"#{page(entry)}"
|
62
|
+
when Entry::TYPE[:note]
|
63
|
+
"_Note:_ #{entry.text.strip}"
|
64
|
+
else
|
65
|
+
"#{entry.text.strip}"
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def item_desc(entry)
|
70
|
+
return entry.desc unless entry.desc_parsed?
|
71
|
+
|
72
|
+
case entry.type
|
73
|
+
when Entry::TYPE[:bookmark]
|
74
|
+
time(entry)
|
75
|
+
else
|
76
|
+
(type(entry) + " @ " + page(entry) + " " + time(entry)).strip
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def page(entry)
|
81
|
+
((entry.page.nil? ? "" : "page #{entry.page}, ") +
|
82
|
+
(entry.loc.nil? ? "" : "loc. #{entry.loc}")).delete_suffix(", ")
|
83
|
+
end
|
84
|
+
|
85
|
+
def time(entry)
|
86
|
+
@config["time"] ? "[#{entry.time}]" : ""
|
87
|
+
end
|
88
|
+
|
89
|
+
def type(entry)
|
90
|
+
SINGULAR[entry.type]
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
data/lib/fyodor/output_writer.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require "fyodor/
|
1
|
+
require "fyodor/output_generator"
|
2
2
|
|
3
3
|
module Fyodor
|
4
4
|
class OutputWriter
|
@@ -12,7 +12,7 @@ module Fyodor
|
|
12
12
|
def write_all
|
13
13
|
puts "\nWriting to #{@output_dir}..." unless @library.empty?
|
14
14
|
@library.each do |book|
|
15
|
-
content =
|
15
|
+
content = OutputGenerator.new(book, @config).content
|
16
16
|
File.open(path(book), "w") { |f| f.puts(content) }
|
17
17
|
end
|
18
18
|
end
|
@@ -21,11 +21,13 @@ module Fyodor
|
|
21
21
|
private
|
22
22
|
|
23
23
|
def path(book)
|
24
|
-
|
24
|
+
basename = book.basename.gsub(/[?*:|\/"<>]/,"_")
|
25
|
+
extension = @config["extension"]
|
26
|
+
path = @output_dir + "#{basename}.#{extension}"
|
25
27
|
|
26
28
|
i = 2
|
27
29
|
while(path.exist?)
|
28
|
-
path = @output_dir + "
|
30
|
+
path = @output_dir + "${basename - #{i}.#{extension}"
|
29
31
|
i += 1
|
30
32
|
end
|
31
33
|
|
data/lib/fyodor/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fyodor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rafael Cavalcanti
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-02-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: toml
|
@@ -56,7 +56,7 @@ files:
|
|
56
56
|
- lib/fyodor/entry.rb
|
57
57
|
- lib/fyodor/entry_parser.rb
|
58
58
|
- lib/fyodor/library.rb
|
59
|
-
- lib/fyodor/
|
59
|
+
- lib/fyodor/output_generator.rb
|
60
60
|
- lib/fyodor/output_writer.rb
|
61
61
|
- lib/fyodor/stats_printer.rb
|
62
62
|
- lib/fyodor/strings.rb
|
data/lib/fyodor/md_generator.rb
DELETED
@@ -1,96 +0,0 @@
|
|
1
|
-
require "fyodor/strings"
|
2
|
-
|
3
|
-
module Fyodor
|
4
|
-
class MdGenerator
|
5
|
-
include Strings
|
6
|
-
|
7
|
-
def initialize(book, config)
|
8
|
-
@book = book
|
9
|
-
@config = config
|
10
|
-
end
|
11
|
-
|
12
|
-
def content
|
13
|
-
header + body + bookmarks
|
14
|
-
end
|
15
|
-
|
16
|
-
|
17
|
-
private
|
18
|
-
|
19
|
-
def header
|
20
|
-
return <<~EOF
|
21
|
-
# #{@book.title}
|
22
|
-
#{"by #{@book.author}" unless @book.author.to_s.empty?}
|
23
|
-
|
24
|
-
#{header_counts}
|
25
|
-
|
26
|
-
EOF
|
27
|
-
end
|
28
|
-
|
29
|
-
def header_counts
|
30
|
-
output = ""
|
31
|
-
@book.count_types.each do |type, n|
|
32
|
-
output += "#{n} #{pluralize(type, n)}, " if n > 0
|
33
|
-
end
|
34
|
-
output.delete_suffix(", ")
|
35
|
-
end
|
36
|
-
|
37
|
-
def pluralize(type, n)
|
38
|
-
n == 1 ? SINGULAR[type] : PLURAL[type]
|
39
|
-
end
|
40
|
-
|
41
|
-
def body
|
42
|
-
entries = @book.reject { |entry| entry.type == Entry::TYPE[:bookmark] }
|
43
|
-
entries.size == 0 ? "" : entries_render(entries)
|
44
|
-
end
|
45
|
-
|
46
|
-
def bookmarks
|
47
|
-
bookmarks = @book.select { |entry| entry.type == Entry::TYPE[:bookmark] }
|
48
|
-
bookmarks.size == 0 ? "" : entries_render(bookmarks, "Bookmarks")
|
49
|
-
end
|
50
|
-
|
51
|
-
def entries_render(entries, title=nil)
|
52
|
-
output = "---\n\n"
|
53
|
-
output += "## #{title}\n\n" unless title.nil?
|
54
|
-
entries.each do |entry|
|
55
|
-
output += "#{item_text(entry)}\n\n"
|
56
|
-
output += "<p style=\"text-align: right;\"><sup>#{item_desc(entry)}</sup></p>\n\n"
|
57
|
-
end
|
58
|
-
output
|
59
|
-
end
|
60
|
-
|
61
|
-
def item_text(entry)
|
62
|
-
case entry.type
|
63
|
-
when Entry::TYPE[:bookmark]
|
64
|
-
"* #{page(entry)}"
|
65
|
-
when Entry::TYPE[:note]
|
66
|
-
"* _Note:_\n#{entry.text.strip}"
|
67
|
-
else
|
68
|
-
"* #{entry.text.strip}"
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
def item_desc(entry)
|
73
|
-
return entry.desc unless entry.desc_parsed?
|
74
|
-
|
75
|
-
case entry.type
|
76
|
-
when Entry::TYPE[:bookmark]
|
77
|
-
time(entry)
|
78
|
-
else
|
79
|
-
(type(entry) + " @ " + page(entry) + " " + time(entry)).strip
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
def page(entry)
|
84
|
-
((entry.page.nil? ? "" : "page #{entry.page}, ") +
|
85
|
-
(entry.loc.nil? ? "" : "loc. #{entry.loc}")).delete_suffix(", ")
|
86
|
-
end
|
87
|
-
|
88
|
-
def time(entry)
|
89
|
-
@config["time"] ? "[#{entry.time}]" : ""
|
90
|
-
end
|
91
|
-
|
92
|
-
def type(entry)
|
93
|
-
SINGULAR[entry.type]
|
94
|
-
end
|
95
|
-
end
|
96
|
-
end
|