rouge-gtk_theme_loader 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 57586e5fb29565f7a61c93ee686d6c898048f04461661f8a32138cf07f230069
4
+ data.tar.gz: d14294fb6202b3f6b9b743955d8c5ae6ab253741a51f804c17b2324385472c56
5
+ SHA512:
6
+ metadata.gz: 7c8dbb36aa1433860f435aa41ba835d51287038c3777b8d9687d835b6dfebf8bf67c7bf40edd351b607183d23edc2b7edb7b03944eb92535f31853ec14057e87
7
+ data.tar.gz: 2ff85d462b66b0960c95ffd40fac97da175df5710909529817f96afaa4d886332ffc63d5dbb81e2b9a2e6e257b46d2a70ac4bc4b232b0e7863ae23579f277270
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Vidar Hokstad
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Rouge::GtkThemeLoader
2
+
3
+ This will attempt to load your GtkSourceView themes into Rouge.
4
+
5
+ ## Installation
6
+
7
+ Install the gem and add to the application's Gemfile by executing:
8
+
9
+ $ bundle add rouge-gtk_theme_loader
10
+
11
+ ## Usage
12
+
13
+ Require Rouge first. Then require 'rouge/gtk_theme_loader' and it will attempt to load any installed
14
+ GtkSourceView themes into Rouge. If you want to install your own custom GtkSourceView
15
+ themes, you can install them in ~/.config/rouge/themes/
16
+
17
+ ## Development
18
+
19
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
20
+
21
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
22
+
23
+ ## Contributing
24
+
25
+ Bug reports and pull requests are welcome on GitHub at https://github.com/vidarh/rouge-gtk_theme_loader.
26
+
27
+ ## License
28
+
29
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ task default: %i[]
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rouge
4
+ class GtkThemeLoader
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,130 @@
1
+ #
2
+ # # GtkThemeLoader
3
+ #
4
+ # Tries to load different GtkSourceView themes
5
+ # as Rouge themes
6
+ #
7
+
8
+ require 'nokogiri'
9
+ require_relative
10
+
11
+ module Rouge
12
+ class GtkThemeLoader
13
+
14
+ include Rouge::Token::Tokens
15
+
16
+ THEME_PATHS=[
17
+ "/usr/share/gtksourceview-2.0/styles/*.xml",
18
+ "/usr/share/gtksourceview-3.0/styles/*.xml",
19
+ "/usr/share/gtksourceview-4/styles/*.xml",
20
+ "~/.config/rouge/themes/*.xml"
21
+ ]
22
+
23
+ # Mapping of GTK source view mappings.
24
+ GTKMAPPING = {
25
+ "def:string" => Literal::String,
26
+ "def:constant" => Literal,
27
+ "def:number" => Literal::Number,
28
+ "def:variable" => Name::Variable,
29
+ "def:keyword" => Keyword,
30
+ "def:statement" => Keyword,
31
+ "def:comment" => Comment,
32
+ "def:type" => [Name::Constant, Name::Namespace],
33
+ "def:identifier" => Name::Builtin,
34
+ "def:emphasis" => Generic::Emph,
35
+ "def:preprocessor" => Name::Builtin,
36
+ }
37
+
38
+ def self.load!
39
+ THEME_PATHS.each do |path|
40
+ Dir[File.expand_path(path).to_s].each do |theme|
41
+ GtkThemeLoader.load(theme)
42
+ end
43
+ end
44
+ end
45
+
46
+ class GtkTheme < Rouge::CSSTheme
47
+ def self.other_styles
48
+ @other_styles ||= {}
49
+ end
50
+
51
+ def other_styles
52
+ self.class.other_styles
53
+ end
54
+ end
55
+
56
+ # Attempt to get a color that's not
57
+ # entirely unreadable when a theme is stupid
58
+ # Will fail horribly for all kinds of values.
59
+ def self.contrast(col)
60
+ r,g,b = col[1..2].to_i(16), col[3..4].to_i(16), col[5..6].to_i(16)
61
+ rn,gn,bn = 255-r, 255-g,255-b
62
+ "#%02x%02x%02x" % [rn,gn,bn]
63
+ end
64
+
65
+ def self.load(theme)
66
+ xml = Nokogiri.XML(File.read(theme))
67
+ theme = Class.new(ReTheme)
68
+ name = xml.xpath("/style-scheme").attr("id").value
69
+ theme.name(name)
70
+
71
+ xml.xpath("//color").each do |c|
72
+ theme.palette(c.attr("name").to_sym => c.attr("value").to_sym)
73
+ end
74
+
75
+ # Rouge barfs if we don't have a default style, so let's make sure
76
+ @text = xml.xpath("//style[@name='text']").first
77
+ if @text
78
+
79
+ theme.style(Text,
80
+ :fg => @text.attr("foreground").to_sym,
81
+ :bg => @text.attr("background").to_sym
82
+ )
83
+ else
84
+ bgp = xml.xpath("//style[@name='background-pattern']")
85
+ if bgp
86
+ bg = bgp.attr("background").value
87
+ end
88
+ bg ||= "#000000"
89
+ if bg[0] == ?#
90
+ fg = contrast(bg)
91
+ end
92
+ theme.style(Text, :fg => fg, :bg => bg, :underline => true)
93
+ end
94
+
95
+ xml.xpath("//style").each do |node|
96
+ name = node.attr("name")
97
+
98
+ use = node.attr("use-style")
99
+ if use && GTKMAPPING[use]
100
+ # name = use
101
+ end
102
+ rougetype = GTKMAPPING[name]
103
+ fg = node.attr("foreground")
104
+ fg = @text.attr("foreground") if @text && !fg
105
+ fg = fg.to_sym if fg && fg[0] != '#'
106
+ bg = node.attr("background")
107
+ fg = @text.attr("background") if @text && !fg
108
+ bg = bg.to_sym if bg && bg[0] != '#'
109
+ opts = {}
110
+ opts[:bg] = bg if bg
111
+ if fg
112
+ opts[:fg] = fg
113
+ end
114
+
115
+ opts[:italic] = true if node.attr("italic").to_s == "true"
116
+ opts[:bold] = true if node.attr("bold").to_s == "true"
117
+ opts[:underline] = true if node.attr("underline")&.to_s == "true"
118
+
119
+ if name.index(":") != nil
120
+ Array(rougetype).each do |rt|
121
+ theme.style(rt, opts)
122
+ end
123
+ end
124
+ theme.other_styles[name] = opts
125
+ end
126
+ end
127
+ end
128
+ end
129
+
130
+ Rouge::GtkThemeLoader.load!
@@ -0,0 +1,6 @@
1
+ module Rouge
2
+ module GtkThemeLoader
3
+ VERSION: String
4
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
5
+ end
6
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rouge-gtk_theme_loader
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Vidar Hokstad
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-10-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rouge
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ description:
28
+ email:
29
+ - vidar@hokstad.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - LICENSE.txt
35
+ - README.md
36
+ - Rakefile
37
+ - lib/rouge/gtk_theme_loader.rb
38
+ - lib/rouge/gtk_theme_loader/version.rb
39
+ - sig/rouge/gtk_theme_loader.rbs
40
+ homepage: https://github.com/vidarh/rouge-gtk_theme_loader
41
+ licenses:
42
+ - MIT
43
+ metadata:
44
+ homepage_uri: https://github.com/vidarh/rouge-gtk_theme_loader
45
+ post_install_message:
46
+ rdoc_options: []
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: 2.6.0
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubygems_version: 3.4.10
61
+ signing_key:
62
+ specification_version: 4
63
+ summary: Load installed GtkSourceView themes into Rouge
64
+ test_files: []