owickstrom / pandoc-include-code

A Pandoc filter for including code from source files
Mozilla Public License 2.0
139 stars 18 forks source link

Allow hyperlinking to original source #23

Closed phillord closed 3 years ago

phillord commented 5 years ago

A feature request! I'd like to be able to hyperlink to the original source in linkable formats (HTML and PDF specifically). So the output would be

```{include=python/hello_world.py}
```

Which would output something that looks like:

print("Hello World") (from hello_world.py)

To make this work portably, there would probably need to be some way of saying where the base link is -- otherwise, links in standalone formats (PDF or standalone HTML) would break.

Just a thought!

aubertc commented 4 years ago

That would be terrific.

aubertc commented 3 years ago

So, this was implemented with https://github.com/owickstrom/pandoc-include-code/pull/36 and merged recently. You can have a look at the documentation at https://github.com/owickstrom/pandoc-include-code#adding-direct-links and https://github.com/owickstrom/pandoc-include-code#adding-base-url-for-all-codeblock-links but let me add a quick example below.

Let's assume you have

print("Hello World")
---
title: A simple test
---

```{include=python/hello_world.py .includeLink}
then

```{.bash}
pandoc --filter pandoc-include-code in.md -o out.html

will give you

image

You can tweak the base of the link (cf. the documentation I've linked before) and tweak the style for html output. For instance, if you have a style.css file:

a.includeCodeLink{text-align: right; display:block; font-style: italic}
a.includeCodeLink::after, a.includeCodeLink::before{display:inline-block; color:black; text-decoration:none;}
a.includeCodeLink::after{content:")"}
a.includeCodeLink::before{content: "(From:\00A0"}

then

pandoc -s --css style.css --filter pandoc-include-code -M base=http://localhost:8000/ in.md -o out.html

(assuming I also want to change the base) would give

image