Skip to content
Permalink
Browse files
Assemble the final .htaccess file from a static file, the Antora gene…
…rated .htaccess file, and transformations of generated 302 rules
  • Loading branch information
djencks committed Nov 16, 2021
1 parent b1a3d3c commit 809e1db85f4461f6e02e3ffee7262bc6285406e9
Showing 6 changed files with 115 additions and 250 deletions.
@@ -1,7 +1,7 @@
site:
title: Apache Camel
url: https://camel.apache.org
start_page: _@manual::index.adoc
# start_page: _@manual::index.adoc

content:
sources:
@@ -97,9 +97,9 @@ ui:
url: ./antora-ui-camel/build/ui-bundle.zip

urls:
redirect_facility: disabled
# latest_version_segment_strategy: redirect:from
# latest_version_segment: latest
redirect_facility: httpd
latest_version_segment_strategy: redirect:from
latest_version_segment: latest


output:
@@ -2,6 +2,7 @@ const cheerio = require('gulp-cheerio');
const env = process.env.CAMEL_ENV || 'development';
const gulp = require('gulp');
const htmlmin = require('gulp-htmlmin');
const inject = require('gulp-inject')

/**
* We minify all HTML files using htmlmin, this is to make them smaller in size
@@ -53,6 +54,44 @@ gulp.task('sitemap', (done) => {
.pipe(gulp.dest('public'));
});

gulp.task('htaccess', (done) => {
return gulp.src(`static/.htaccess`)
.pipe(
inject(
gulp.src('documentation/.htaccess'),
{
starttag:'<!-- inject:htaccess -->',
removeTags: true,
transform: (filename, file) => {
return versionlessRedirects(file.contents.toString('utf8'))
},
}
)
)
.pipe(gulp.dest('public'))
});

const REDIRECT_RX = /^Redirect 302 \/(?<component>c.*)\/latest \/\k<component>\/(?<version>.*)$/

function versionlessRedirects (text) {
const lines = text.split('\n')
const processed = lines.reduce((accum, line) => {
accum.push(line)
const m = line.match(REDIRECT_RX)
if (m) {
accum.push(`RedirectMatch 302 "^/${m.groups.component}(/?)$" "/${m.groups.component}/${m.groups.version}/"`)
// The first line redirects **/next to **/next/ so the second line does not match.
// Apparently it needs to be a match or it will transform **/next/ to **/next//
accum.push(`RedirectMatch 301 "^/${m.groups.component}/next$" "/${m.groups.component}/next/"`)
accum.push(`RedirectMatch 302 "^/${m.groups.component}/(?![0-9].*|next/)(.+)$" "/${m.groups.component}/${m.groups.version}/$1"`)
// As an alternative, the following line works as long as no file names start with 'next'
// accum.push(`RedirectMatch 302 "^/${m.groups.component}/(?![0-9].*|next)(.+)$" "/${m.groups.component}/${m.groups.version}/$1"`)
}
return accum
}, [])
return processed.join('\n')
}

/*
* Removes the content from the `public` directory.
*/
@@ -0,0 +1,9 @@
#!/bin/sh

# Run httpd in docker

#docker build -t camel-website .
docker run --rm -p 80:80 -p 443:443 \
-v "$PWD/public":/usr/local/apache2/htdocs/:Z \
-v "$PWD/support/http":/support:Z \
httpd:2.4 /bin/bash -c "cp /support/* /usr/local/apache2/conf/ && httpd-foreground"
@@ -3,12 +3,13 @@
"version": "1.0.0-SNAPSHOT",
"license": "Apache-2.0",
"scripts": {
"build": "run-s build:antora build:hugo build:sitemap build:minify",
"build": "run-s build:antora build:hugo build:sitemap build:htaccess build:minify",
"build-all": "yarn workspaces foreach --topological-dev run build",
"build:antora": "antora --clean --fetch antora-playbook.yml --stacktrace",
"build:hugo": "hugo --cacheDir ${HUGO_CACHE_DIR:-$(pwd)/.hugo_data} ${HUGO_OPTIONS:-}",
"build:minify": "gulp minify",
"build:sitemap": "gulp sitemap",
"build:htaccess": "gulp htaccess",
"check:html": "html-validate public",
"check:links": "deadlinks-linux public",
"checks": "run-s check:links check:html",
@@ -38,6 +39,7 @@
"gulp": "~4.0",
"gulp-cheerio": "~1.0",
"gulp-htmlmin": "~5.0",
"gulp-inject": "^5.0.2",
"html-validate": "~4",
"hugo-extended": "^0.84.3",
"netlify-cli": "^6.14.10",

0 comments on commit 809e1db

Please sign in to comment.