Fix relinker

This fixes several flaws in the link rewriter:
- broken links on the docs side
- multiple links on one line being mangled
- byzantine logic

Also generalize the logic of the Dockerfile so it is no longer specific to "registry" (env variable), which is a first step in making it possible to upstream all this into the base image and docs project.

Added a number of tests (test.md) to validate the link replacer behavior (against test.compare.md), and embedded the test so that the doc build will fail if the link replacer does not behave.

This is still sed, unfortunately.

Signed-off-by: Olivier Gambier <olivier@docker.com>
This commit is contained in:
Olivier Gambier 2015-08-04 22:55:21 -07:00
parent a0c63372fa
commit 3ad11ff363
3 changed files with 48 additions and 19 deletions

View File

@ -1,26 +1,23 @@
FROM docs/base:latest
MAINTAINER Mary Anthony <mary@docker.com> (@moxiegirl)
ENV PROJECT=registry
# To get the git info for this repo
COPY . /src
COPY . /docs/content/registry/
COPY . /docs/content/$PROJECT/
# Sed to process GitHub Markdown
# 1-2 Remove comment code from metadata block
# 3 Change ](/word to ](/project/ in links
# 4 Change ](word.md) to ](/project/word)
# 5 Remove .md extension from link text
# 6 Change ](./ to ](/project/word)
# 7 Change ](../../ to ](/project/
# 8 Change ](../ to ](/project/
#
RUN find /docs/content/registry -type f -name "*.md" -exec sed -i.old \
-e '/^<!.*metadata]>/g' \
-e '/^<!.*end-metadata.*>/g' \
-e 's/\(\]\)\([(]\)\(\/\)/\1\2\/registry\//g' \
-e 's/\(\][(]\)\([A-Za-z0-9]*\)\(\.md\)/\1\/registry\/\2/g' \
-e 's/\([(]\)\(.*\)\(\.md\)/\1\2/g' \
-e 's/\(\][(]\)\(\.\/\)/\1\/registry\//g' \
-e 's/\(\][(]\)\(\.\.\/\.\.\/\)/\1\/registry\//g' \
-e 's/\(\][(]\)\(\.\.\/\)/\1\/registry\//g' {} \;
# Processing GitHub Markdown
# 1 Remove <!--[metadata]> and <![end-metadata]-->
# 2 Remove any leading combination of dots and slashes (./, ../, ../../, etc)
# 3 Prepend /$PROJECT to all links, remove any trailing .md, only for non http:// links and non internal anchors
RUN find /docs/content/$PROJECT -type f -name "*.md" -not -name "*.compare.md" -exec sed -i.old \
-e '/^<!\(--\)\{0,1\}\[\(end-\)\{0,1\}metadata\]\(--\)\{0,1\}>/g' \
-e 's/\(\][(]\)\(\.*\/\)*/\1/g' \
-e 's/\(\][(]\)\([A-Za-z0-9_/-]\{1,\}\)\(\.md\)\{0,1\}\(#\{0,1\}\(#[A-Za-z0-9_-]*\)\{0,1\}\)[)]/\1\/'$PROJECT'\/\2\4)/g' \
{} \;
# Prepare the compare file and expect an empty diff against test.md
RUN sed -i.old -e 's/\/placeholder\//\/'$PROJECT'\//g' /docs/content/$PROJECT/test.compare.md && diff -u /docs/content/$PROJECT/test.md /docs/content/$PROJECT/test.compare.md

16
docs/test.compare.md Normal file
View File

@ -0,0 +1,16 @@
WHATEVER
WHATEVER AGAIN
[display11](/placeholder/link-1) [display12](/placeholder/link_2) [display13](/placeholder/link/3)
[display21](/placeholder/link-1) [display22](/placeholder/link_2) [display23](/placeholder/link/3)
[display31.md](/placeholder/link-1) [display32.md](/placeholder/link_2) [display33.md](/placeholder/link/3)
[display41](/placeholder/link-1) [display42](/placeholder/link_2) [display43](/placeholder/link/3)
[display51](/placeholder/link-1) [display52](/placeholder/link_2) [display53](/placeholder/link/3)
[display61](/placeholder/link-1) [display62](/placeholder/link_2) [display63](/placeholder/link/3)
[display71](/placeholder/link-1#something-else) [display62](/placeholder/link_2#else) [display63](#else)
[dont-touch](https://somewhere/foo.md#dont) [dont-touch](https://somewhere/foo.md#dont)

16
docs/test.md Normal file
View File

@ -0,0 +1,16 @@
<!--[metadata]>
WHATEVER
<![end-metadata]-->
<!--[metadata]>
WHATEVER AGAIN
<![end-metadata]-->
[display11](/link-1) [display12](/link_2) [display13](/link/3)
[display21](link-1.md) [display22](link_2.md) [display23](link/3.md)
[display31.md](link-1.md) [display32.md](link_2.md) [display33.md](link/3.md)
[display41](./link-1.md) [display42](./link_2.md) [display43](./link/3.md)
[display51](../link-1.md) [display52](../link_2.md) [display53](../link/3.md)
[display61](../../link-1.md) [display62](../../link_2.md) [display63](../../link/3.md)
[display71](link-1.md#something-else) [display62](link_2#else) [display63](#else)
[dont-touch](https://somewhere/foo.md#dont) [dont-touch](https://somewhere/foo.md#dont)