Skip to content

Commit

Permalink
adds very sloppy live reloading funcitonality
Browse files Browse the repository at this point in the history
  • Loading branch information
elasticspoon committed Jan 19, 2024
1 parent 1b9fb01 commit 76dab6e
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 20 deletions.
56 changes: 56 additions & 0 deletions assets/reload.js
@@ -0,0 +1,56 @@
const regex = /<meta property="time_built" content="([^"]+)">/;
let lastModified;

async function startReload() {
setInterval(() => {
if (!lastModified) {
setLastModified();
reload();
// console.log("Initial reload");
return;
} else {
// console.log("Checking for reload");
reloadIfOld();
}
}, 1000);
}

function reload() {
fetch("/")
.then((response) => {
return response.text();
})
.then((html) => {
document.body.innerHTML = html;
console.log("Reloaded");
});
}

function reloadIfOld() {
getBuiltTime().then((timeBuilt) => {
// console.log("Last modified: " + lastModified);
if (timeBuilt > lastModified) {
// console.log("Reloading because of new build");
reload();
lastModified = timeBuilt;
}
});
}

function setLastModified() {
getBuiltTime().then((timeBuilt) => {
lastModified = timeBuilt;
});
}

async function getBuiltTime() {
let inputString = await fetch("/");
let text = await inputString.text();
let match = text.match(regex);
if (match && match[1]) {
const timeBuiltString = match[1];
return new Date(timeBuiltString);
}
}

startReload();
9 changes: 4 additions & 5 deletions lib/local-server.rb
Expand Up @@ -15,25 +15,24 @@ def start
end

def filewatcher
puts "watching #{opts.input} and #{opts.css_path}" if opts.verbose
@filewatcher ||= Filewatcher.new([opts.input, opts.css_path])
end

def watch_files
generator.send(:write_html)
@thread = Thread.new(filewatcher) do |fw|
fw.watch do |change|
puts "Change detected: #{change}"
puts "Change detected: #{change}" if opts.verbose
generator.send(:write_html)
end
end
ensure
filewatcher.stop
@thread.kill
end

def start_local_server
puts "Starting local server on port #{opts.port}"
root = opts.html_path
server = WEBrick::HTTPServer.new Port: 8000, DocumentRoot: root
server = WEBrick::HTTPServer.new(Port: opts.port, DocumentRoot: root)

trap 'INT' do server.shutdown end

Expand Down
25 changes: 13 additions & 12 deletions lib/parser.rb
Expand Up @@ -3,8 +3,8 @@

class Parser
class ScriptOptions
attr_accessor :chrome_path, :html, :pdf, :css_path, :pdf_path, :html_path, :verbose, :live_reload, :input,
:serve_only
attr_accessor :chrome_path, :html, :pdf, :css_path, :pdf_path, :html_path, :verbose, :input,
:serve_only, :port

def initialize
self.chrome_path = nil
Expand All @@ -14,9 +14,9 @@ def initialize
self.pdf_path = Pathname.new('resume.pdf').expand_path
self.html_path = Pathname.new('resume.html').expand_path
self.verbose = false
self.live_reload = false
self.serve_only = false
self.input = nil
self.port = 3000
end

def define_options(parser)
Expand All @@ -36,7 +36,7 @@ def define_options(parser)
specify_output_pdf_option(parser)
specify_output_html_option(parser)
specify_input_css_option(parser)
boolean_live_reload_option(parser)
specify_server_port_option(parser)
boolean_serve_only_option(parser)
boolean_verbosity_option(parser)
parser.separator ''
Expand Down Expand Up @@ -78,17 +78,18 @@ def specify_input_css_option(parser)
end
end

def boolean_verbosity_option(parser)
parser.on('-v', '--[no-]verbose', 'Run verbosely') do |v|
self.verbose = v
def specify_server_port_option(parser)
parser.on('--server-port=PORT', 'Specify the localhost port number for the server') do |port|
port = port.to_i
raise OptionParser::InvalidArgument unless (1..65_535).cover?(port)

self.port = port
end
end

def boolean_live_reload_option(parser)
# Boolean switch.
parser.on('--live-reload', 'Start a live dev-server using foreman', 'This rebuild your html output and use livereload to refresh your browser',
'You will need the livereload browser extension for the reloading to work') do |v|
self.live_reload = v
def boolean_verbosity_option(parser)
parser.on('-v', '--[no-]verbose', 'Run verbosely') do |v|
self.verbose = v
end
end

Expand Down
15 changes: 12 additions & 3 deletions lib/resume_generator.rb
Expand Up @@ -10,9 +10,12 @@ class ResumeGenerator
ValueError = Class.new(StandardError)

POSTAMBLE = <<~POSTAMBLE.freeze
</div>
</body>
</html>
</div>
</body>
<script>
#{File.read(File.expand_path('../assets/reload.js', __dir__))}
</script>
</html>
POSTAMBLE

CHROME_GUESSES_MACOS = [
Expand Down Expand Up @@ -157,6 +160,7 @@ def preamable(title, css)
<html lang="en">
<head>
<meta charset="UTF-8">
<meta property="time_built" content="#{Time.now.iso8601}">
<title>#{title}</title>
<style>
#{css}
Expand All @@ -178,6 +182,10 @@ def title(markdown)
'Cannot find any lines that look like markdown h1 headings to use as the title'
end

def reload_script
File.read(File.expand_path('assets/reload.js', __dir__))
end

def make_html(markdown, css_file: nil)
title = title(markdown)
html = Kramdown::Document.new(markdown).to_html
Expand All @@ -192,6 +200,7 @@ def make_html(markdown, css_file: nil)
end

def write_html
puts "Writing HTML to #{opts.html_path}"
File.write(opts.html_path, to_html, mode: 'w') if opts.html
rescue Errno::ENOENT => e
raise e unless e.message =~ /No such file or directory/
Expand Down

0 comments on commit 76dab6e

Please sign in to comment.