pirka 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b62ef7b04eada438c72e2b96ed9121a370422c01
4
- data.tar.gz: f10abfecad074936a7f469ca1beee3b9e0532644
3
+ metadata.gz: 88fcda5b7f58df80108a0eba4386756ccd9ddbaf
4
+ data.tar.gz: 428c8af5035de8b0da4a0fc63507f3b5f8d2f6fe
5
5
  SHA512:
6
- metadata.gz: 026aec15e3d275e63b57ee70db453cd2e0e89b46951b501eb64b635e6d586210677b6d95615cc705d118372dfb673aaf8026d209fb3792c31469947f343cd607
7
- data.tar.gz: bdc08a466ab58d14f3b42e9910d8e58f9e06eb719e836ed3c219c38b387b13ba5197068a49ba75b39c27a0f84e5e2f7b55aad0bba0ec40a3efe6c88dea2539fe
6
+ metadata.gz: 55814a01738772bead120d50677462cc911d72df46621fb43b97747a603d8175d909652cc1e7a00d6b93f4c1398af654017b6b6282b658c17b7975939672515e
7
+ data.tar.gz: 9037389b5d65fa6867a41901102f5e8b91f5cb270e74c57357276159a3aae17dca21107a17e13a5ff8eaa8d03cc1910479fdec54942227a18536878a8bd9f6f4
data/README.md CHANGED
@@ -28,6 +28,8 @@ It's a short cut to:
28
28
 
29
29
  $ pirka highlight path/to/book.epub
30
30
 
31
+ To highlight books, run `pirka update`(see below for details) just after installation because `pirka highlight` requires library files.
32
+
31
33
  ### Detecting source code from EPUB books ###
32
34
 
33
35
  $ pirka detect path/to/book.epub
@@ -71,7 +73,7 @@ You also determine languages interactively. Set `-i`(`--interactive`) option:
71
73
 
72
74
  $ pirka update
73
75
 
74
- Pirka provides official library files for some EPUB books as Git repository((https://gitlab.com/KitaitiMakoto/pirka-library)[https://gitlab.com/KitaitiMakoto/pirka-library]). `pirka update` command fethes the files from the repository and you benefit from it.
76
+ Pirka provides official library files for some EPUB books as Git repository([https://gitlab.com/KitaitiMakoto/pirka-library](https://gitlab.com/KitaitiMakoto/pirka-library)). `pirka update` command fethes the files from the repository and you benefit from it.
75
77
 
76
78
  Additionally, you can host library files by your own and make Pirka recognizes it by configuration file. See later section for that.
77
79
 
@@ -92,24 +94,24 @@ Pirka can be configured by environment variables, config file and command-line o
92
94
 
93
95
  #### Environment variables ####
94
96
 
95
- XDG_DATA_HOME
97
+ `XDG_DATA_HOME`
96
98
  : Affects directory to save library files.
97
99
  : Library files are saved to `$XDG_DATA_HOME/pirka/local`
98
100
  : The directory is used to search library, too.
99
101
  : Default: `$HOME/.local/share`
100
102
 
101
- XDG_DATA_DIRS
103
+ `XDG_DATA_DIRS`
102
104
  : Affects directory to save library files.
103
105
  : You can specify multiple directory by seperating with a colon like `XDG_DATA_DIRS=/dir1:/dir2`.
104
106
  : `/dir1/pirka/local` and `/dir2/pirka/local` are used to search library, for example.
105
107
  : Default: `/usr/local/share:/usr/share`
106
108
 
107
- XDG_CONFIG_HOME
109
+ `XDG_CONFIG_HOME`
108
110
  : Affects directory to search and save config file.
109
111
  : `$XDG_CONFIG_DIRS/pirka.yaml` is recognized as config file.
110
112
  : Default: `$HOME/.config`
111
113
 
112
- XDG_CONFIG_DIRS
114
+ `XDG_CONFIG_DIRS`
113
115
  : Affects directory to search config file.
114
116
  : You can specify multiple directory by seperating with a colon like `XDG_CONFIG_DIRS=/dir1:/dir2`.
115
117
  : `/dir1/pirka.yaml` and `/dir2/pirka.yaml` are searched as config file.
@@ -119,16 +121,16 @@ XDG_CONFIG_DIRS
119
121
 
120
122
  Config file is a YAML file. Properties below are recognized:
121
123
 
122
- data_home
124
+ `data_home`
123
125
  : Directory to save and search library files.
124
126
  : Default: `$XDG_CONFIG_HOME/pirka/local`
125
127
 
126
- additional_directories
128
+ `additional_directories`
127
129
  : Directories to search library files.
128
130
  : Expressed by sequence(array).
129
131
  : Default: `[]`
130
132
 
131
- library_repositories
133
+ `library_repositories`
132
134
  : Git repository URIs used by `pirka lib` command.
133
135
  : Expressed by sequence(array).
134
136
  : Default: `[]`
data/app/highlight.rb CHANGED
@@ -6,6 +6,7 @@ require "epub/maker"
6
6
  require "rouge"
7
7
  require "rouge/lexers/fluentd"
8
8
  require "pirka/library"
9
+ require "pirka/highlighter"
9
10
  require_relative "subcommand"
10
11
 
11
12
  module Pirka
@@ -20,7 +21,7 @@ module Pirka
20
21
  DUMMY_ORIGIN = Addressable::URI.parse("file:///")
21
22
  CSS_PATH = "pirka/style.css" # @todo Avoid conflict with existing item by other than Pirka
22
23
  CSS_CLASS_NAME = "pirka"
23
- SCOPE = "code.#{CSS_CLASS_NAME}"
24
+ SCOPE = ".#{CSS_CLASS_NAME}"
24
25
  THEME = "github"
25
26
 
26
27
  def initialize(config)
@@ -79,7 +80,17 @@ module Pirka
79
80
  def highlight_contents(epub, css_item, library)
80
81
  need_save = []
81
82
 
82
- formatter = Rouge::Formatters::HTML.new
83
+ highlighter = Highlighter::Middleware::ClassName.new(
84
+ Highlighter::Middleware::Rouge.new(
85
+ Highlighter.new),
86
+ class_name: CSS_CLASS_NAME)
87
+ middleware = library.metadata["middleware"]
88
+ if middleware && !library.metadata["middleware"].empty?
89
+ highlighter = library.metadata["middleware"].reduce(highlighter) {|highlighter, desc|
90
+ params = desc["params"] || {}
91
+ Highlighter::Middleware.const_get(desc["name"]).new(highlighter, params)
92
+ }
93
+ end
83
94
 
84
95
  library.each.reverse_each do |(cfi, data)|
85
96
  lang = data["language"]
@@ -90,18 +101,8 @@ module Pirka
90
101
  itemref, elem, _ = EPUB::Searcher.search_by_cfi(epub, cfi)
91
102
  item = itemref.item
92
103
  doc = elem.document
93
- lexer = Rouge::Lexer.find(lang) || Rouge::Lexer.guess(source: elem.content)
94
- unless lexer
95
- warn "Cannot find lexer for #{lang}"
96
- next
97
- end
98
- elem.inner_html = formatter.format(lexer.lex(elem.content)) # @todo Consider the case `elem` has descendants
99
104
 
100
- classes = (elem["class"] || "").split(/\s+/)
101
- unless classes.include? CSS_CLASS_NAME
102
- classes << CSS_CLASS_NAME
103
- elem["class"] = classes.join(" ")
104
- end
105
+ highlighter.markup elem, lang
105
106
 
106
107
  link = doc.at('#pirka') # @todo Avoid conflict with existing link
107
108
  unless link
@@ -0,0 +1,73 @@
1
+ require "rouge"
2
+ require "rouge/lexers/fluentd"
3
+
4
+ module Pirka
5
+ class Highlighter
6
+ def markup(element, lang)
7
+ # noop
8
+ end
9
+
10
+ class Middleware
11
+ class Rouge
12
+ FORMATTER = ::Rouge::Formatters::HTML.new
13
+
14
+ def initialize(highlighter, formatter: FORMATTER)
15
+ @highlighter = highlighter
16
+ @formatter = formatter
17
+ end
18
+
19
+ def markup(element, lang)
20
+ @highlighter.markup(element, lang)
21
+ lexer = ::Rouge::Lexer.find(lang) || ::Rouge::Lexer.guess(source: element.content)
22
+ unless lexer
23
+ warn "Cannot find lexer for #{lang}"
24
+ return
25
+ end
26
+ element.inner_html = @formatter.format(lexer.lex(element.content)) # @todo Consider the case `element` has descendants
27
+ end
28
+ end
29
+
30
+ class ClassName
31
+ ATTR_NAME = "class"
32
+ ATTR_SEPARATOR = /\s+/
33
+ CLASS_NAME = "pirka"
34
+
35
+ def initialize(highlighter, class_name: CLASS_NAME)
36
+ @highlighter = highlighter
37
+ @class_name = class_name
38
+ end
39
+
40
+ def markup(element, lang)
41
+ @highlighter.markup element, lang
42
+ class_names = (element[ATTR_NAME] || "").split(ATTR_SEPARATOR)
43
+ return if class_names.include? @class_name
44
+ class_names << @class_name
45
+ element[ATTR_NAME] = class_names.join(" ")
46
+ end
47
+ end
48
+
49
+ class LineNum
50
+ def initialize(highlighter, params = {})
51
+ @highlighter = highlighter
52
+ @selector = params["selector"]
53
+ raise "selector param not specified" unless @selector
54
+ end
55
+
56
+ def markup(element, lang)
57
+ nums = element.search(@selector)
58
+ nums.each(&:unlink)
59
+ @highlighter.markup element, lang
60
+ return if nums.empty?
61
+ element.inner_html = element.inner_html.lines.collect.with_index {|line, index|
62
+ num = nums[index].to_xml
63
+ if line.length > 1
64
+ line[0..0] << num << line[1..-1]
65
+ else
66
+ num << line
67
+ end
68
+ }.join
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
data/lib/pirka/version.rb CHANGED
@@ -17,6 +17,6 @@
17
17
 
18
18
  module Pirka
19
19
  # pirka version
20
- VERSION = "0.1.0"
20
+ VERSION = "0.1.1"
21
21
  EPUB_PARSER_VERSION = "0.3.1"
22
22
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pirka
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - KITAITI Makoto
@@ -258,6 +258,7 @@ files:
258
258
  - bin/pirka
259
259
  - lib/pirka.rb
260
260
  - lib/pirka/config.rb
261
+ - lib/pirka/highlighter.rb
261
262
  - lib/pirka/library.rb
262
263
  - lib/pirka/version.rb
263
264
  - pirka.gemspec