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

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)