Skip to content

Commit c1320b0

Browse files
committed
Render optgroup for light / dark syntax highlighting
1 parent 5ee0cbb commit c1320b0

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-19
lines changed

app/models/settings/syntax_highlight.rb

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,34 @@ def default
77
end
88

99
def find(name)
10-
if available_names.include?(name)
11-
new(name: name)
10+
attrs = curated_data.find { |attrs| attrs[:name] == name }
11+
if attrs.present?
12+
new(**attrs)
1213
else
1314
raise ActiveRecord::RecordNotFound, "Couldn't find SyntaxHighlight with name '#{name}'"
1415
end
1516
end
1617

1718
def curated
18-
available_names.map { |name| find(name) }
19+
curated_data.map { |attrs| new(**attrs) }
1920
end
2021

21-
def available_names
22-
@available_names ||= Dir.glob(css_glob_pattern).map { |file| File.basename(file, ".css") }
23-
end
24-
25-
private
26-
27-
def css_glob_pattern
28-
Rails.root.join("app", "assets", "stylesheets", "pygments", "*.css")
22+
def curated_data
23+
@curated_data ||= YAML.load_file(Rails.root.join("config", "syntax_highlights.yml"))
2924
end
3025
end
3126

32-
attr_accessor :name
27+
attr_accessor :name, :mode
3328

34-
def initialize(name:)
29+
def initialize(name:, mode:, **)
3530
@name = name
31+
@mode = mode
3632
end
3733

3834
def ==(other)
3935
return false if other.nil? || !other.is_a?(self.class)
4036

41-
name == other.name
37+
name == other.name && mode == other.mode
4238
end
4339

4440
def path

app/views/settings/syntax_highlights/form.rb

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,17 @@ def view_template
1414
stylesheet_link_tag @current_highlight.asset_path, data: {syntax_highlight: @current_highlight.name}
1515

1616
div(class: "grid grid-content", data: {controller: "syntax-highlight-preview", syntax_highlight_preview_name_value: @current_highlight.name}) do
17-
p {
17+
p do
1818
plain %(Current syntax highlight style:)
19+
whitespace
1920
strong { @current_highlight.name }
20-
}
21+
end
2122
form_with(
2223
model: @current_highlight,
2324
url: settings_syntax_highlight_path,
2425
method: :get
2526
) do |form|
26-
fieldset {
27+
fieldset do
2728
form.label :name, "Choose a syntax highlight style:"
2829
form.select :name,
2930
syntax_highlight_options_for_select,
@@ -32,7 +33,7 @@ def view_template
3233
},
3334
name: "settings[syntax_highlight]",
3435
onchange: "this.form.requestSubmit()"
35-
}
36+
end
3637
end
3738

3839
h2 { %(Preview) }
@@ -76,6 +77,8 @@ def each(&)
7677
private
7778

7879
def syntax_highlight_options_for_select
79-
@available_highlights.map { |highlight| [highlight.name, highlight.name] }
80+
@available_highlights
81+
.group_by { |sh| sh.mode }
82+
.map { |mode, syntaxes| [mode.titleize, syntaxes.map { |sh| [sh.name.titleize, sh.name] }] }
8083
end
8184
end

0 commit comments

Comments
 (0)