From 9e8acf257d2335c05fcd3a7aa56816f5739233a2 Mon Sep 17 00:00:00 2001 From: Hendrik Kleinwaechter Date: Mon, 11 Dec 2023 23:36:22 +0100 Subject: [PATCH] Validate website format (#304) * Validate website format This validates the format of the website and throws an error if something is fishy. * Remove -j flag * clean build * Clean cache in between --- .github/workflows/release-book-website.yml | 32 +++++++++++----------- .github/workflows/test-book-website.yml | 16 +++++------ website/modify_build.rb | 17 ++++++++++++ 3 files changed, 41 insertions(+), 24 deletions(-) diff --git a/.github/workflows/release-book-website.yml b/.github/workflows/release-book-website.yml index 85a4600..03c3f32 100644 --- a/.github/workflows/release-book-website.yml +++ b/.github/workflows/release-book-website.yml @@ -49,22 +49,6 @@ jobs: aws_bucket: ${{ secrets.AWS_BUCKET_BOOK }} source_dir: book/release destination_dir: release - - name: Bake the website - uses: addnab/docker-run-action@v3 - with: - image: ${{ env.IMAGE }} - options: -v ${{ github.workspace }}:/app - run: | - cd /app/book - make website - - name: Release baked website to S3 - uses: shallwefootball/s3-upload-action@master - with: - aws_key_id: ${{ secrets.AWS_KEY_ID }} - aws_secret_access_key: ${{ secrets.AWS_SECRET_ACCESS_KEY}} - aws_bucket: ${{ secrets.AWS_BUCKET_WEBSITE }} - source_dir: website/static_website_html - destination_dir: static_html_root - name: Upload book Artifacts uses: actions/upload-artifact@v3 with: @@ -73,6 +57,22 @@ jobs: book/book_serif/book.log book/book_serif/book.pdf book/book-epub/book.epub + - name: Bake the website + uses: addnab/docker-run-action@v3 + with: + image: ${{ env.IMAGE }} + options: -v ${{ github.workspace }}:/app + run: | + cd /app/book + make mrproper && make website + - name: Release baked website to S3 + uses: shallwefootball/s3-upload-action@master + with: + aws_key_id: ${{ secrets.AWS_KEY_ID }} + aws_secret_access_key: ${{ secrets.AWS_SECRET_ACCESS_KEY}} + aws_bucket: ${{ secrets.AWS_BUCKET_WEBSITE }} + source_dir: website/static_website_html + destination_dir: static_html_root - name: Upload website Artifacts uses: actions/upload-artifact@v3 with: diff --git a/.github/workflows/test-book-website.yml b/.github/workflows/test-book-website.yml index 8afd33f..24c9e2e 100644 --- a/.github/workflows/test-book-website.yml +++ b/.github/workflows/test-book-website.yml @@ -40,14 +40,6 @@ jobs: run: | cd /app/book make -j build_serif_pdf build_ebook - - name: Test building website - uses: addnab/docker-run-action@v3 - with: - image: ${{ env.IMAGE }} - options: -v ${{ github.workspace }}:/app - run: | - cd /app/book - make -j website - name: Upload book Artifacts uses: actions/upload-artifact@v3 with: @@ -56,6 +48,14 @@ jobs: book/book_serif/book.log book/book_serif/book.pdf book/book-epub/book.epub + - name: Test building website + uses: addnab/docker-run-action@v3 + with: + image: ${{ env.IMAGE }} + options: -v ${{ github.workspace }}:/app + run: | + cd /app/book + make mrproper && make website - name: Upload website Artifacts uses: actions/upload-artifact@v3 with: diff --git a/website/modify_build.rb b/website/modify_build.rb index e7604d4..8f527fe 100644 --- a/website/modify_build.rb +++ b/website/modify_build.rb @@ -5,6 +5,8 @@ require 'nokogiri' # several optimisations on the HTML. Nokogiri is used to facilitate the # modifications. +class InvalidWebsiteFormat < StandardError; end + class ModifyBuild HOST = "https://www.the-sourdough-framework.com".freeze @@ -15,6 +17,8 @@ class ModifyBuild def build build_latex_html create_sitemap + rescue InvalidWebsiteFormat => e + raise e end private @@ -42,6 +46,7 @@ class ModifyBuild def modify_file(filename) orig_text = File.read(filename, encoding: "UTF-8") + validate_file(orig_text) text = fix_double_slashes(orig_text) text = fix_navigation_bar(text) text = fix_titles(text) @@ -101,6 +106,18 @@ class ModifyBuild text.gsub(/\/\//, "/") end + # Sometimes for whatever reason the make4ht input produces files that are + # improperly formatted. This validator will go through the files and do a + # couple of basic checks to see if the files are in the format we expect. If + # not an exception is caused. + def validate_file(text) + doc = build_doc(text) + stylesheets = doc.css("link[rel='stylesheet']").map{|attr| attr["href"] } + has_all_styles = %w(book.css style.css).all? { |required_stylesheet| stylesheets.include?(required_stylesheet) } + raise InvalidWebsiteFormat.new("No style tag style.css found in the website") unless has_all_styles + true + end + def fix_navigation_bar(text) doc = build_doc(text) elements = [doc.search('.chapterToc'), doc.search('.sectionToc'), doc.search('.subsectionToc')].flatten