Skip to content

Commit

Permalink
Merge pull request #152 from ReflectsLight/tidy
Browse files Browse the repository at this point in the history
Add Nanoc::Filters::Tidy
  • Loading branch information
0x1eef authored Oct 7, 2023
2 parents 9c19ef1 + c274c1a commit afd6e3d
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/al-quran.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,7 @@ jobs:
uses: actions/checkout@v2
- name: Prepare environment
uses: './.github/actions/prepare-env'
- name: Add HTMLTidy
run: sudo apt-get install tidy
- name: nanoc
run: bundle exec rake build
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@ nginx, apache, etc).
<strong>The stack</strong>
</p>

The following languages and tools have to be installed before
the website can be built:

* Ruby 3.1, or later.
* NodeJS v18.15, or later.
* TypeScript
* [tidy-html5](https://github.com/htacg/tidy-html5) <br>
There is a good chance there's a package available for
tidy-html5 on your operating system of choice.

<p align="left">
<strong>Local development</strong>
Expand All @@ -27,7 +32,7 @@ __1. Clone__
git clone https://github.com/ReflectsLight/al-quran.git
cd al-quran

__2. Install Ruby, and NodeJS packages__
__2. Install Ruby, NodeJS packages__

bundle install
npm i
Expand Down
48 changes: 48 additions & 0 deletions nanoc/lib/nanoc/filters/tidy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# frozen_string_literal: true

class Nanoc::Filters::Tidy < Nanoc::Filter
require "fileutils"
include FileUtils
Error = Class.new(RuntimeError)

identifier :tidy
type text: :text

def self.default_options
@default_options ||= {wrap: 120, indent: true}
end

def run(content, options = {})
file = temporary_file_for(content)
tidy file, self.class.default_options.merge(options)
end

private

def tidy(file, options)
system "tidy", "-modify", "-quiet", *tidy_args(options), file.path
if $?.success?
File.read(file.path).tap { file.tap(&:unlink).close }
else
raise Error, "tidy exited unsuccessfully (exit code: #{$?.exitstatus})", []
end
end

def tidy_args(options)
options.each_with_object([]) do |(key, value), ary|
if value.equal?(true)
ary << "-#{key}"
else
ary.concat ["-#{key}", value.to_s]
end
end
end

def temporary_file_for(content)
dir = File.join(Dir.getwd, "tmp", "htmltidy")
mkdir_p(dir) unless Dir.exist?(dir)
file = Tempfile.new(File.basename(item.identifier.to_s), dir)
file.write(content)
file.tap(&:flush)
end
end
1 change: 1 addition & 0 deletions nanoc/rules/pages/surah/index.rules
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ locales.each do |locale|
compile "/html/pages/surah/index.html.erb", rep: "/#{locale}/surah/index" do
context = Ryo.from(locale:)
filter(:erb, locals: {context:})
filter(:tidy)
write "/#{locale}/index.html"
end
end
Expand Down
1 change: 1 addition & 0 deletions nanoc/rules/pages/surah/stream.rules
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Ryo.each(slugs) do |id, slug|
surah: {id:, name:, slug:}
)
filter(:erb, locals: {context:})
filter(:tidy)
write "/#{locale}/#{identifier}/index.html"
end
locales.each do |locale|
Expand Down

0 comments on commit afd6e3d

Please sign in to comment.