From 3ad11ff36372a483dc18123c90b49cf4bc0ee047 Mon Sep 17 00:00:00 2001 From: Olivier Gambier Date: Tue, 4 Aug 2015 22:55:21 -0700 Subject: [PATCH] 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 --- docs/Dockerfile | 35 ++++++++++++++++------------------- docs/test.compare.md | 16 ++++++++++++++++ docs/test.md | 16 ++++++++++++++++ 3 files changed, 48 insertions(+), 19 deletions(-) create mode 100644 docs/test.compare.md create mode 100644 docs/test.md diff --git a/docs/Dockerfile b/docs/Dockerfile index 0ed4e5266..a1b9a14af 100644 --- a/docs/Dockerfile +++ b/docs/Dockerfile @@ -1,26 +1,23 @@ FROM docs/base:latest MAINTAINER Mary Anthony (@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 '/^/g' \ - -e '/^/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' {} \; \ No newline at end of file +# Processing GitHub Markdown +# 1 Remove +# 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 '/^/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 diff --git a/docs/test.compare.md b/docs/test.compare.md new file mode 100644 index 000000000..86087a15c --- /dev/null +++ b/docs/test.compare.md @@ -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) diff --git a/docs/test.md b/docs/test.md new file mode 100644 index 000000000..3c139cb30 --- /dev/null +++ b/docs/test.md @@ -0,0 +1,16 @@ + + + +[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)