mirror of
https://github.com/hendricius/the-sourdough-framework
synced 2025-11-08 20:21:12 -06:00
🎉 Book Website (#145)
* Basic script to modify html output * Modify HTML of website * Add makefile * Compile website in CI * Improve readme
This commit is contained in:
committed by
GitHub
parent
dc2b810743
commit
d5889a67a1
1
website/.ruby-version
Normal file
1
website/.ruby-version
Normal file
@@ -0,0 +1 @@
|
||||
3.1.2
|
||||
8
website/Gemfile
Normal file
8
website/Gemfile
Normal file
@@ -0,0 +1,8 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
source 'https://rubygems.org'
|
||||
|
||||
ruby File.read('.ruby-version').strip
|
||||
|
||||
gem 'nokogiri'
|
||||
gem 'pry'
|
||||
27
website/Gemfile.lock
Normal file
27
website/Gemfile.lock
Normal file
@@ -0,0 +1,27 @@
|
||||
GEM
|
||||
remote: https://rubygems.org/
|
||||
specs:
|
||||
coderay (1.1.3)
|
||||
method_source (1.0.0)
|
||||
nokogiri (1.15.3-arm64-darwin)
|
||||
racc (~> 1.4)
|
||||
nokogiri (1.15.3-x86_64-linux)
|
||||
racc (~> 1.4)
|
||||
pry (0.14.2)
|
||||
coderay (~> 1.1)
|
||||
method_source (~> 1.0)
|
||||
racc (1.7.1)
|
||||
|
||||
PLATFORMS
|
||||
arm64-darwin-22
|
||||
x86_64-linux
|
||||
|
||||
DEPENDENCIES
|
||||
nokogiri
|
||||
pry
|
||||
|
||||
RUBY VERSION
|
||||
ruby 3.1.2p20
|
||||
|
||||
BUNDLED WITH
|
||||
2.4.12
|
||||
5
website/makefile
Normal file
5
website/makefile
Normal file
@@ -0,0 +1,5 @@
|
||||
.PHONY: website
|
||||
website:
|
||||
cd ../book && make mrproper && make website
|
||||
bundle install
|
||||
ruby modify_build.rb
|
||||
82
website/modify_build.rb
Normal file
82
website/modify_build.rb
Normal file
@@ -0,0 +1,82 @@
|
||||
require 'pry'
|
||||
require 'nokogiri'
|
||||
|
||||
# This class goes through the generated default LaTeX HTML and performs
|
||||
# several optimisations on the HTML. Nokogiri is used to facilitate the
|
||||
# modifications.
|
||||
|
||||
class ModifyBuild
|
||||
def self.build
|
||||
new.build
|
||||
end
|
||||
|
||||
def build
|
||||
build_latex_html
|
||||
end
|
||||
|
||||
def build_latex_html
|
||||
system("rm -rf #{build_dir}/")
|
||||
system("mkdir #{build_dir}/")
|
||||
copy_source_to_local_dir_for_modification
|
||||
list_of_files_to_modify.each do |filename|
|
||||
modify_file(filename)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def modify_file(filename)
|
||||
orig_text = File.read(filename)
|
||||
text = fix_double_slashes(orig_text)
|
||||
text = fix_navigation_bar(text)
|
||||
File.open(filename, "w") {|file| file.puts text }
|
||||
end
|
||||
|
||||
def list_of_files_to_modify
|
||||
Dir.glob("#{build_dir}/*.html")
|
||||
end
|
||||
|
||||
def copy_source_to_local_dir_for_modification
|
||||
system("cd ../book/ && make website") unless source_website_output_exists?
|
||||
system("cp -R ../book/#{build_dir}/* #{build_dir}")
|
||||
end
|
||||
|
||||
def source_website_output_exists?
|
||||
File.directory?("../book/#{build_dir}/")
|
||||
end
|
||||
|
||||
def build_dir
|
||||
'static_website_html'
|
||||
end
|
||||
|
||||
def fix_double_slashes(text)
|
||||
text.gsub(/\/\//, "/")
|
||||
end
|
||||
|
||||
def fix_navigation_bar(text)
|
||||
doc = Nokogiri::HTML(text)
|
||||
elements = [doc.search('.chapterToc'), doc.search('.sectionToc'), doc.search('.subsectionToc')].flatten
|
||||
elements.each do |n|
|
||||
chapter_number_or_nothing = n.children[0].text.strip
|
||||
hyperlink_node = n.children[1]
|
||||
# remove unneeded text and merge into single a tag
|
||||
n.children[0].remove
|
||||
link_text = hyperlink_node.content
|
||||
# no chapter number
|
||||
if chapter_number_or_nothing.length == 0
|
||||
content = hyperlink_node.to_s
|
||||
else
|
||||
link_node_content = %Q{
|
||||
<span class="chapter_number">#{chapter_number_or_nothing}</span>
|
||||
<span class="link_text">#{link_text}</span>
|
||||
}
|
||||
hyperlink_node.inner_html = link_node_content
|
||||
content = hyperlink_node.to_s
|
||||
end
|
||||
n.inner_html = content
|
||||
end
|
||||
doc.to_html
|
||||
end
|
||||
end
|
||||
|
||||
ModifyBuild.build
|
||||
Reference in New Issue
Block a user