From 834da53058069fe50da510fa86e0807a7ff7868f Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Mon, 24 May 2021 10:17:37 -0700 Subject: Add `rebase_relative_paths` extension. - Add manual entry for (non-default) extension `rebase_relative_paths`. - Add constructor `Ext_rebase_relative_paths` to `Extensions` in Text.Pandoc.Extensions [API change]. When enabled, this extension rewrites relative image and link paths by prepending the (relative) directory of the containing file. - Make Markdown reader sensitive to the new extension. - Add tests for #3752. Closes #3752. NB. currently the extension applies to markdown and associated readers but not commonmark/gfm. --- test/command/chap1/spider.png | Bin 0 -> 63531 bytes test/command/chap1/text.md | 11 +++++++++++ 2 files changed, 11 insertions(+) create mode 100644 test/command/chap1/spider.png create mode 100644 test/command/chap1/text.md (limited to 'test/command/chap1') diff --git a/test/command/chap1/spider.png b/test/command/chap1/spider.png new file mode 100644 index 000000000..7ee9fe339 Binary files /dev/null and b/test/command/chap1/spider.png differ diff --git a/test/command/chap1/text.md b/test/command/chap1/text.md new file mode 100644 index 000000000..88b30313d --- /dev/null +++ b/test/command/chap1/text.md @@ -0,0 +1,11 @@ +# Chapter one + +A spider: ![spider](spider.png) + +The moon: ![moon](../../lalune.jpg) + +Link to [spider picture](spider.png). + +URL left alone: [manual](https://pandoc.org/MANUAL.html). + +Absolute path left alone: [absolute](/foo/bar/baz.png). -- cgit v1.2.3 From 6972a7dc9158c1d56b2dc9a06f596386f6b30860 Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Thu, 27 May 2021 11:26:38 -0700 Subject: Modify rebase_reference_links treatment of reference links/images. The directory is based on the file containing the link reference, not the file containing the link, if these differ. --- MANUAL.txt | 6 +++++- src/Text/Pandoc/Readers/Markdown.hs | 9 ++++----- test/command/3752.md | 5 ++++- test/command/chap1/text.md | 2 ++ test/command/chap2/text.md | 2 ++ 5 files changed, 17 insertions(+), 7 deletions(-) (limited to 'test/command/chap1') diff --git a/MANUAL.txt b/MANUAL.txt index 2ca50e44a..d755c0c00 100644 --- a/MANUAL.txt +++ b/MANUAL.txt @@ -5080,7 +5080,11 @@ Without this extension, you would have to use `![image](chap1/spider.jpg)` in `chap1/text.md` and `![image](chap2/spider.jpg)` in `chap2/text.md`. Links with relative paths will be rewritten in the same way as images. -*This option currently only affects Markdown input.* + +Note that relative paths in reference links and images will +be rewritten relative to the file containing the link +reference definition, not the file containing the reference link +or image itself, if these differ. #### Extension: `attributes` #### diff --git a/src/Text/Pandoc/Readers/Markdown.hs b/src/Text/Pandoc/Readers/Markdown.hs index 968c6c165..e5cbadc94 100644 --- a/src/Text/Pandoc/Readers/Markdown.hs +++ b/src/Text/Pandoc/Readers/Markdown.hs @@ -336,7 +336,9 @@ referenceKey = try $ do notFollowedBy' (() <$ reference) many1Char $ notFollowedBy space >> litChar let betweenAngles = try $ char '<' >> manyTillChar litChar (char '>') - src <- try betweenAngles <|> sourceURL + rebase <- option False (True <$ guardEnabled Ext_rebase_relative_paths) + src <- (if rebase then rebasePath pos else id) <$> + (try betweenAngles <|> sourceURL) tit <- option "" referenceTitle attr <- option nullAttr $ try $ do guardEnabled Ext_link_attributes @@ -1857,8 +1859,6 @@ referenceLink constructor (lab, raw) = do return (mempty, ""))) <|> try ((guardDisabled Ext_spaced_reference_links <|> spnl) >> reference) - rebase <- option False (True <$ guardEnabled Ext_rebase_relative_paths) - pos <- getPosition when (raw' == "") $ guardEnabled Ext_shortcut_reference_links let labIsRef = raw' == "" || raw' == "[]" let key = toKey $ if labIsRef then raw else raw' @@ -1884,8 +1884,7 @@ referenceLink constructor (lab, raw) = do Nothing -> makeFallback else makeFallback Just ((src,tit), attr) -> - let src' = if rebase then rebasePath pos src else src - in constructor attr src' tit <$> lab + constructor attr src tit <$> lab dropBrackets :: Text -> Text dropBrackets = dropRB . dropLB diff --git a/test/command/3752.md b/test/command/3752.md index 76d51989b..6d31eb450 100644 --- a/test/command/3752.md +++ b/test/command/3752.md @@ -2,12 +2,14 @@ % pandoc command/chap1/text.md command/chap2/text.md -f markdown+rebase_relative_paths --verbose -t docx | pandoc -f docx -t plain ^D [INFO] Loaded command/chap1/spider.png from ./command/chap1/spider.png -[INFO] Loaded command/chap1/../../lalune.jpg from ./command/chap1/../../lalune.jpg [INFO] Loaded command/chap2/spider.png from ./command/chap2/spider.png +[INFO] Loaded command/chap1/../../lalune.jpg from ./command/chap1/../../lalune.jpg Chapter one A spider: [spider] +Another spider: [another spider] + The moon: [moon] Link to spider picture. @@ -26,6 +28,7 @@ A spider: [spider] ^D

Chapter one

A spider: spider

+

Another spider: another spider

The moon: moon

Link to spider picture.

URL left alone: manual.

diff --git a/test/command/chap1/text.md b/test/command/chap1/text.md index 88b30313d..d25514241 100644 --- a/test/command/chap1/text.md +++ b/test/command/chap1/text.md @@ -2,6 +2,8 @@ A spider: ![spider](spider.png) +Another spider: ![another spider][refspider] + The moon: ![moon](../../lalune.jpg) Link to [spider picture](spider.png). diff --git a/test/command/chap2/text.md b/test/command/chap2/text.md index 435a266d7..082a1d79e 100644 --- a/test/command/chap2/text.md +++ b/test/command/chap2/text.md @@ -1,3 +1,5 @@ # Chapter two A spider: ![spider](spider.png) + +[refspider]: spider.png -- cgit v1.2.3 From 0661ce699fe83c64dd5a5874e8ab17241b19550c Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Thu, 27 May 2021 13:53:26 -0700 Subject: rebase_relative_paths extension: don't change fragment paths. We don't want a pure fragment path to be rewritten, since these are used for cross-referencing. --- MANUAL.txt | 3 +++ src/Text/Pandoc/Readers/Markdown.hs | 3 ++- test/command/3752.md | 3 +++ test/command/chap1/text.md | 2 ++ 4 files changed, 10 insertions(+), 1 deletion(-) (limited to 'test/command/chap1') diff --git a/MANUAL.txt b/MANUAL.txt index d755c0c00..fb1a76e9a 100644 --- a/MANUAL.txt +++ b/MANUAL.txt @@ -5081,6 +5081,9 @@ Without this extension, you would have to use `![image](chap2/spider.jpg)` in `chap2/text.md`. Links with relative paths will be rewritten in the same way as images. +Absolute paths and URLs are not changed. Neither are paths +consisting entirely of a fragment, e.g. `[section one](#foo)`. + Note that relative paths in reference links and images will be rewritten relative to the file containing the link reference definition, not the file containing the reference link diff --git a/src/Text/Pandoc/Readers/Markdown.hs b/src/Text/Pandoc/Readers/Markdown.hs index e5cbadc94..74f2668e4 100644 --- a/src/Text/Pandoc/Readers/Markdown.hs +++ b/src/Text/Pandoc/Readers/Markdown.hs @@ -1923,7 +1923,8 @@ autoLink = try $ do rebasePath :: SourcePos -> Text -> Text rebasePath pos path = do let fp = sourceName pos - in if isAbsolute (T.unpack path) || isURI path + isFragment = T.take 1 path == "#" + in if isFragment || isAbsolute (T.unpack path) || isURI path then path else case takeDirectory fp of diff --git a/test/command/3752.md b/test/command/3752.md index 6d31eb450..2765d79ac 100644 --- a/test/command/3752.md +++ b/test/command/3752.md @@ -18,6 +18,8 @@ URL left alone: manual. Absolute path left alone: absolute. +Link to fragment: chapter two. + Chapter two A spider: [spider] @@ -33,6 +35,7 @@ A spider: [spider]

Link to spider picture.

URL left alone: manual.

Absolute path left alone: absolute.

+

Link to fragment: chapter two.

Chapter two

A spider: spider

``` diff --git a/test/command/chap1/text.md b/test/command/chap1/text.md index d25514241..68a317161 100644 --- a/test/command/chap1/text.md +++ b/test/command/chap1/text.md @@ -11,3 +11,5 @@ Link to [spider picture](spider.png). URL left alone: [manual](https://pandoc.org/MANUAL.html). Absolute path left alone: [absolute](/foo/bar/baz.png). + +Link to fragment: [chapter two](#chapter-two). -- cgit v1.2.3 From 4b16d181e7219ed161a0e03c0c5ee9dec4b526b4 Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Thu, 27 May 2021 14:16:37 -0700 Subject: rebase_relative_paths: leave empty paths unchanged. --- MANUAL.txt | 4 ++-- src/Text/Pandoc/Readers/Markdown.hs | 2 +- test/command/3752.md | 3 +++ test/command/chap1/text.md | 2 ++ 4 files changed, 8 insertions(+), 3 deletions(-) (limited to 'test/command/chap1') diff --git a/MANUAL.txt b/MANUAL.txt index fb1a76e9a..c234debc9 100644 --- a/MANUAL.txt +++ b/MANUAL.txt @@ -5081,8 +5081,8 @@ Without this extension, you would have to use `![image](chap2/spider.jpg)` in `chap2/text.md`. Links with relative paths will be rewritten in the same way as images. -Absolute paths and URLs are not changed. Neither are paths -consisting entirely of a fragment, e.g. `[section one](#foo)`. +Absolute paths and URLs are not changed. Neither are empty +paths or paths consisting entirely of a fragment, e.g., `#foo`. Note that relative paths in reference links and images will be rewritten relative to the file containing the link diff --git a/src/Text/Pandoc/Readers/Markdown.hs b/src/Text/Pandoc/Readers/Markdown.hs index 74f2668e4..bc5e3e30f 100644 --- a/src/Text/Pandoc/Readers/Markdown.hs +++ b/src/Text/Pandoc/Readers/Markdown.hs @@ -1924,7 +1924,7 @@ rebasePath :: SourcePos -> Text -> Text rebasePath pos path = do let fp = sourceName pos isFragment = T.take 1 path == "#" - in if isFragment || isAbsolute (T.unpack path) || isURI path + in if T.null path || isFragment || isAbsolute (T.unpack path) || isURI path then path else case takeDirectory fp of diff --git a/test/command/3752.md b/test/command/3752.md index 2765d79ac..df8af0ba1 100644 --- a/test/command/3752.md +++ b/test/command/3752.md @@ -20,6 +20,8 @@ Absolute path left alone: absolute. Link to fragment: chapter two. +Empty path: empty. + Chapter two A spider: [spider] @@ -36,6 +38,7 @@ A spider: [spider]

URL left alone: manual.

Absolute path left alone: absolute.

Link to fragment: chapter two.

+

Empty path: empty.

Chapter two

A spider: spider

``` diff --git a/test/command/chap1/text.md b/test/command/chap1/text.md index 68a317161..54f0104de 100644 --- a/test/command/chap1/text.md +++ b/test/command/chap1/text.md @@ -13,3 +13,5 @@ URL left alone: [manual](https://pandoc.org/MANUAL.html). Absolute path left alone: [absolute](/foo/bar/baz.png). Link to fragment: [chapter two](#chapter-two). + +Empty path: [empty](). -- cgit v1.2.3 From e86f6abc45b8192eb98ec6d0a3b65d2a3c6726ad Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Sat, 29 May 2021 12:06:32 -0700 Subject: Further test image size reductions. --- test/command/chap1/spider.png | Bin 63531 -> 8758 bytes test/command/chap2/spider.png | Bin 9861 -> 2921 bytes 2 files changed, 0 insertions(+), 0 deletions(-) (limited to 'test/command/chap1') diff --git a/test/command/chap1/spider.png b/test/command/chap1/spider.png index 7ee9fe339..4c6bf13e9 100644 Binary files a/test/command/chap1/spider.png and b/test/command/chap1/spider.png differ diff --git a/test/command/chap2/spider.png b/test/command/chap2/spider.png index 5377e940b..5456a857a 100644 Binary files a/test/command/chap2/spider.png and b/test/command/chap2/spider.png differ -- cgit v1.2.3