public inbox archive for pandoc-discuss@googlegroups.com
 help / color / mirror / Atom feed
From: BP Jonsson <bpjonsson-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org,
	Alon Bar-Lev
	<alon.barlev-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
Subject: Re: [markdown] links between documents
Date: Thu, 7 Feb 2019 21:18:44 +0100	[thread overview]
Message-ID: <a2366d6c-8f01-2119-1c93-30404f689d25@gmail.com> (raw)
In-Reply-To: <CAOazyz2k3FYHaF4SjVX0agZDH=4ZSAKm6QYyTppVvaQymnbXJA-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>

[-- Attachment #1: Type: text/plain, Size: 4070 bytes --]

Den 2019-02-07 kl. 07:26, skrev Alon Bar-Lev:
> Thanks!
> But then the markdown will not be able to process the files as separate files.
> For example, I use markdown generation online on top of git, so
> references should be between files and expose this to users. But when
> I create a PDF or HTML out of all the files using pandoc, I expect the
> generation to be aware of the files it reads and convert the links
> that matches the files into inline references.

I have attached a filter which hopefully solves your problems.

It seems to me you actually got two problems here:

1.  If you generate one big file with the whole text you want internal
    targets like `#title-b` but when you generate separate files from
    each source file containing a major section you want external
    targets like `b.html`, `b.pdf` or even `b.html#b-title`.

2.  You probably want the file extensions in your external targets to be
    the one appropriate for each generated file format rather than the
    file extension of your source files.

Luckily both problems are easy to solve with a filter or two:

1.  Since Link elements take attributes in recent versions of Pandoc you
    can put the internal target in the target slot of your link elements
    and add the external target as a custom attribute, i.e. instead of
    `[here](b.html)` *or* `[here](#b-title)` you type
    `[here](#b-title){file=b.html}` and let a filter replace the
    internal target in the target slot with the external target in the
    attribute when generating multi-file output. (Theoretically you
    could do it the other way around, but its hard to come up with a
    good attribute name as short as `file` in that case! :-)

2.  Here you can have a filter replace the `.md` extension --- or even a
    dummy extension like `.foo` --- with the appropriate extension for
    each output format.

Ideally you want a configurable filter. Filters can be configured either
through document metadata --- which can also be set on the command line
with the `-M` option --- or with environment variables. I usually
provide for both so that I can have the general case configured in the
environment and then override it with metadata as needed.

So in this filter you have the following configuration variables:

| Metadata key    | Purpose                                 | Default |
|:----------------|:----------------------------------------|:--------|
| `file_urls`     | Replace internal targets with external? | false   |
| `file_url_attr` | The attribute to use for file urls.     | `file`  |
| `dummy_ext`     | The extension which should be replaced. | `md`    |
| `real_ext`      | The extension to replace it with.       | `html`  |

All the environment variable names are derived from the metadata keys by
capitalizing them and prepending the prefix `PANDOC_CROSSURLS_`, so
`PANDOC_CROSSURLS_FILE_URLS` and so on. Since the `file_urls` variable
is a boolean the environment variable value has to be converted to one:
the value `false` (case insensitive) counts as false, all other defined
values as true. Undefined/unset means that the metadata value wins.

So when generating a single file (with internal links) you just say

    pandoc --lua-filter pandoc-crossurls.lua -M file_urls=false -o combined.pdf *.md

and when generating a separate file you say

    pandoc --lua-filter pandoc-crossurls.lua -M file_urls -M real_ext=pdf -o a.pdf a.md

-- 
You received this message because you are subscribed to the Google Groups "pandoc-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pandoc-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To post to this group, send email to pandoc-discuss-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit https://groups.google.com/d/msgid/pandoc-discuss/a2366d6c-8f01-2119-1c93-30404f689d25%40gmail.com.
For more options, visit https://groups.google.com/d/optout.

[-- Attachment #2: pandoc-crossurls.lua.zip --]
[-- Type: application/zip, Size: 2607 bytes --]

  parent reply	other threads:[~2019-02-07 20:18 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-02-06 21:00 Alon Bar-Lev
     [not found] ` <CAOazyz04jcRfqkGEkpb2FcBmjVJ7C=7cvSf-Tm1wKBWeCKEuoQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2019-02-07  0:13   ` BPJ
     [not found]     ` <CADAJKhCnRDa+CWqntyPrvqfpSmO+Zakb8hj==C0XxPzBNeBJZQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2019-02-07  6:26       ` Alon Bar-Lev
     [not found]         ` <CAOazyz2k3FYHaF4SjVX0agZDH=4ZSAKm6QYyTppVvaQymnbXJA-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2019-02-07 20:18           ` BP Jonsson [this message]
     [not found]             ` <a2366d6c-8f01-2119-1c93-30404f689d25-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2019-02-08 16:35               ` Alon Bar-Lev

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=a2366d6c-8f01-2119-1c93-30404f689d25@gmail.com \
    --to=bpjonsson-re5jqeeqqe8avxtiumwx3w@public.gmane.org \
    --cc=alon.barlev-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org \
    --cc=pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).