Skip to content

Commit cf69f1e

Browse files
committed
Add syntax form to preview other syntax highlights
1 parent ac83529 commit cf69f1e

File tree

11 files changed

+190
-1
lines changed

11 files changed

+190
-1
lines changed

app/controllers/application_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ class ApplicationController < ActionController::Base
22
include Erroring
33
include Authentication
44
include ColorScheming
5+
include SyntaxHighlighting
56
end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module SyntaxHighlighting
2+
extend ActiveSupport::Concern
3+
4+
included do
5+
helper_method :find_syntax_highlight
6+
end
7+
8+
def find_syntax_highlight
9+
@syntax_highlight ||= preview_syntax_highlight || session_syntax_highlight || default_syntax_highlight
10+
end
11+
12+
def preview_syntax_highlight_id = params.fetch(:settings, {}).permit(:syntax_highlight)[:syntax_highlight]
13+
14+
def session_syntax_highlight_id = session[:syntax_highlight]
15+
16+
def preview_syntax_highlight = @preview_syntax_highlight ||= preview_syntax_highlight_id && Settings::SyntaxHighlight.find(preview_syntax_highlight_id)
17+
18+
def session_syntax_highlight = @session_syntax_highlight ||= session_syntax_highlight_id && Settings::SyntaxHighlight.find(session_syntax_highlight_id)
19+
20+
def default_syntax_highlight = @default_syntax_highlight ||= Settings::SyntaxHighlight.default
21+
end
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Settings::SyntaxHighlightsController < ApplicationController
2+
def show
3+
render Settings::SyntaxHighlights::ShowView.new(
4+
current_highlight: find_syntax_highlight,
5+
available_highlights: Settings::SyntaxHighlight.curated
6+
)
7+
end
8+
9+
def update
10+
# highlight = params[:syntax_highlight]
11+
# if available_highlight_styles.include?(highlight)
12+
# current_user.update(syntax_highlight: highlight)
13+
# flash[:notice] = "Syntax highlight updated to #{highlight}"
14+
# else
15+
# flash[:alert] = "Invalid syntax highlight choice"
16+
# end
17+
# redirect_to action: :show
18+
19+
syntax_highlight = params.fetch(:settings, {}).permit(:syntax_highlight)
20+
@syntax_highlight = Settings::SyntaxHighlight.find(syntax_highlight)
21+
22+
redirect_to settings_syntax_highlight_path unless @syntax_highlight.present?
23+
24+
if @syntax_highlight == Settings::SyntaxHighlight.default
25+
session.delete(:syntax_highlight)
26+
else
27+
session[:syntax_highlight] = @syntax_highlight.name
28+
end
29+
30+
redirect_to settings_syntax_highlight_path, status: :see_other
31+
end
32+
end
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
class Settings::SyntaxHighlight
2+
include ActiveModel::Model
3+
4+
class << self
5+
def default
6+
find("dracula")
7+
end
8+
9+
def find(name)
10+
if available_names.include?(name)
11+
new(name: name)
12+
else
13+
raise ActiveRecord::RecordNotFound, "Couldn't find SyntaxHighlight with name '#{name}'"
14+
end
15+
end
16+
17+
def curated
18+
available_names.map { |name| find(name) }
19+
end
20+
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")
29+
end
30+
end
31+
32+
attr_accessor :name
33+
34+
def initialize(name:)
35+
@name = name
36+
end
37+
38+
def ==(other)
39+
return false if other.nil? || !other.is_a?(self.class)
40+
41+
name == other.name
42+
end
43+
44+
def path
45+
Rails.root.join("app", "assets", "stylesheets", "pygments", "#{name}.css")
46+
end
47+
48+
def asset_path
49+
"pygments/#{name}.css"
50+
end
51+
end

app/views/application/_footer.html.erb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
<li class="">
1818
<%= link_to "Settings", settings_color_scheme_path %>
1919
</li>
20+
<li class="">
21+
<%= link_to "Syntax Highlight", settings_syntax_highlight_path %>
22+
</li>
2023
<!--
2124
<li>
2225
<ul>
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
<%= vite_stylesheet_tag "entrypoints/code/dracula.css", media: "all", "data-turbo-track": "reload" %>
1+
<%#= vite_stylesheet_tag "entrypoints/code/dracula.css", media: "all", "data-turbo-track": "reload" %>
2+
<%= stylesheet_link_tag find_syntax_highlight.asset_path, media: "all", "data-turbo-track": "reload" %>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div id="example_<%= example.basename(".*") %>">
2+
<%= render CodeBlock::AppFile.new(example.app_path, language: "ruby") %>
3+
</div>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
class Settings::SyntaxHighlights::Form < ApplicationView
2+
include Phlex::Rails::Helpers::FormWith
3+
include Phlex::Rails::Helpers::LinkTo
4+
include Phlex::Rails::Helpers::ButtonTo
5+
include Phlex::Rails::Helpers::ContentFor
6+
include Phlex::Rails::Helpers::StylesheetLinkTag
7+
8+
def initialize(current_highlight:, available_highlights:)
9+
@current_highlight = current_highlight
10+
@available_highlights = available_highlights
11+
end
12+
13+
def view_template
14+
div(class: "grid grid-content") do
15+
# if @current_highlight != Settings::SyntaxHighlight.default
16+
stylesheet_link_tag @current_highlight.asset_path
17+
# end
18+
19+
p {
20+
plain %(Current syntax highlight style:)
21+
strong { @current_highlight.name }
22+
}
23+
form_with(
24+
model: @current_highlight,
25+
url: settings_syntax_highlight_path,
26+
method: :get
27+
) do |form|
28+
fieldset {
29+
form.label :name, "Choose a syntax highlight style:"
30+
form.select :name,
31+
syntax_highlight_options_for_select,
32+
{
33+
selected: @current_highlight.name
34+
},
35+
name: "settings[syntax_highlight]",
36+
onchange: "this.form.requestSubmit()"
37+
}
38+
end
39+
40+
h2 { %(Preview) }
41+
h3 { %(Ruby) }
42+
render CodeBlock::AppFile.new("app/controllers/application_controller.rb", language: "ruby")
43+
h3 { %(CSS) }
44+
render CodeBlock::AppFile.new("app/javascript/css/components/page-header.css", language: "css")
45+
h3 { %(HTML with ERB) }
46+
render CodeBlock::AppFile.new("app/views/layouts/application.html.erb", language: "erb")
47+
end
48+
end
49+
50+
private
51+
52+
def syntax_highlight_options_for_select
53+
@available_highlights.map { |highlight| [highlight.name, highlight.name] }
54+
end
55+
end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Settings::SyntaxHighlights::ShowView < ApplicationView
2+
include Phlex::Rails::Helpers::TurboFrameTag
3+
4+
def initialize(current_highlight:, available_highlights:)
5+
@current_highlight = current_highlight
6+
@available_highlights = available_highlights
7+
end
8+
9+
def view_template
10+
render Pages::Header.new(title: "Settings: Syntax Highlight")
11+
12+
section(class: %(secton-content container py-gap)) do
13+
turbo_frame_tag "syntax-highlight-form" do
14+
render Settings::SyntaxHighlights::Form.new(
15+
current_highlight: @current_highlight,
16+
available_highlights: @available_highlights
17+
)
18+
end
19+
end
20+
end
21+
end

0 commit comments

Comments
 (0)