Skip to content

Commit 74d1ec3

Browse files
committed
wip: Hackily added the ability to live-reload stylesheets in static mode.
1 parent 3d30600 commit 74d1ec3

File tree

3 files changed

+28
-19
lines changed

3 files changed

+28
-19
lines changed

app/controllers/static_site/entries_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def show
1010
serve_blob(blob)
1111

1212
elsif @entry.manifest?
13-
render plain: @entry.body
13+
render plain: @entry.render_stylesheet!, content_type: 'text/css'
1414

1515
# TODO: make up my mind on how to handle templates.
1616
# elsif @entry.template?

app/models/ad_hoc_markdown_importer.rb

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ def entry_attributes_from_document(identifier, file_path)
103103
if identifier == "stylesheets/application.css.scss"
104104
# TODO: should this also be a "template"? doesn't super matter.
105105
entry_kind = :manifest
106-
entry_body = File.read(file_path)
107106
elsif identifier =~ /\.erb$/
108107
# idea is that templates are rendered from within context of a
109108
# controller, which is too painful to setup here
@@ -140,29 +139,15 @@ def process_templates(notebook)
140139
# if there is a stylesheets/application.css.scss we want to render the
141140
# Sass and convert it to a stylesheets/application.css
142141
if stylesheet = notebook.entries.manifests.find_by(identifier: "stylesheets/application.css.scss")
143-
load_path = File.join(notebook.import_path, "stylesheets")
144-
145-
rendered_css = SassC::Engine.new(stylesheet.body, {
146-
filename: "application.css.scss",
147-
syntax: :scss,
148-
load_paths: [load_path],
149-
}).render
150-
151142
# there can only be ONE application.css
152143
if to_delete = notebook.entries.find_by(identifier: "stylesheets/application.css")
153144
puts "Destroying extraneous stylesheets/application.css, so it can be replaced."
154145
to_delete.destroy
155146
end
156147

157-
rendered_stylesheet = notebook.entries.new(stylesheet.export_attributes)
158-
rendered_stylesheet.identifier = "stylesheets/application.css"
159-
rendered_stylesheet.kind = :document
160-
rendered_stylesheet.save!
161-
162-
blob = ActiveStorage::Blob.create_and_upload!(io: StringIO.new(rendered_css),
163-
filename: "application.css")
164-
blob.analyze
165-
rendered_stylesheet.files.create(blob_id: blob.id, created_at: blob.created_at)
148+
stylesheet.identifier = "stylesheets/application.css"
149+
stylesheet.render_stylesheet!
150+
stylesheet.save
166151
end
167152
end
168153

app/models/entry.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,4 +442,28 @@ def self.accepted_attributes
442442
"state",
443443
"hide"]
444444
end
445+
446+
# ---- hack
447+
SCSS_MANIFEST = "application.css.scss"
448+
def render_stylesheet!
449+
if self.manifest?
450+
load_path = File.join(parent_notebook.import_path, "stylesheets")
451+
manifest_path = File.join(load_path, SCSS_MANIFEST)
452+
453+
if File.exist?(manifest_path)
454+
if body.nil? || File.mtime(manifest_path) > updated_at
455+
rendered_css = SassC::Engine.new(File.read(manifest_path), {
456+
filename: SCSS_MANIFEST,
457+
syntax: :scss,
458+
load_paths: [load_path],
459+
}).render
460+
461+
self.body = rendered_css
462+
self.save!
463+
end
464+
end
465+
end
466+
467+
self.body
468+
end
445469
end

0 commit comments

Comments
 (0)