public inbox archive for pandoc-discuss@googlegroups.com
 help / color / mirror / Atom feed
From: BPJ <melroch-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
To: pandoc-discuss <pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
Subject: Re: Output-Format Plain removes Markdown-Hyperlinks []()
Date: Sat, 15 Jan 2022 15:44:51 +0100	[thread overview]
Message-ID: <90b5b9c0-3597-372e-5436-99a6f0c41d2f@gmail.com> (raw)
In-Reply-To: <c26e9460-27b6-ed81-2701-e1f3dcbac293-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>

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

Ack I forgot the attachments! Typical... :-)

On 2022-01-15 15:41, BPJ wrote:
> Here with pandoc 2.16.2 `[TEXT](URL)` is automatically turned into `TEXT 
> <URL>` in plain output, being smart enough to do the right thing if the 
> URL appears in the link text or if it was `<URL>` already.
> 
> Anyway attached is a Lua filter which I use to get the same effect in 
> other output formats.  Unless you are running a very old version of 
> pandoc it should do what you want unless pandoc does it already.
> 
> On 2022-01-14 12:20, Peter Mueller wrote:
>> Hello together.
>>
>> I'm working on a project where we use one input markdown file to create
>> HTML but also TXT files.
>> The problem I have is with markdown hyperlinks in the form of []().
>>
>> While for the output format html they are kept:
>> *$ echo "prefix [this is a link](www.google.de) suffix" | pandoc -f
>> markdown -t html*
>> *<p>prefix <a href="www.google.de">this is a link</a> suffix</p>*
>>
>> the url is completely removed for plain output:
>> *$ echo "prefix [this is a link](www.google.de) suffix" | pandoc -f
>> markdown -t plain*
>> *prefix this is a link suffix*
>>
>> Is there a way/plugin to keep the url within the plain output?
>> (e.g.  by keeping the markdown  [this is a link](www.google.de) as it is,
>> but handling all other markdown as usual?)
>>
>> Thank you
>> Peter
>>
> 

-- 
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 view this discussion on the web visit https://groups.google.com/d/msgid/pandoc-discuss/90b5b9c0-3597-372e-5436-99a6f0c41d2f%40gmail.com.

[-- Attachment #2: result.html --]
[-- Type: text/html, Size: 293 bytes --]

[-- Attachment #3: plain-links.lua --]
[-- Type: text/x-lua, Size: 1652 bytes --]

-- Pandoc filter which turns `[TEXT](URL)` into `TEXT \<URL>`

-- Save some typing and sidewise space! :-)
local p = assert(pandoc, "Cannot find pandoc library")
local u = assert(p.utils, "Cannot find pandoc.utils library")

-- Classes which Pandoc inserts into links with URL/email as link text
local plain_classes = { 'uri', 'email' }

-- Get a nice-looking URL/email string
local function bare_url (url)
  -- Remove wrapping angles just in case
  url = tostring(url):gsub("^%<", "")
  url = url:gsub("%>$", "")
  -- Remove the mailto protocol if any
  url = url:gsub("^mailto%:", "")
  return url
end

-- Get a pattern which matches a string literally
-- by escaping all punctuation chars in the string
local function quote_pat (str)
  return tostring(str):gsub('%p', '%%%0')
end

-- The main action
function Link (link)
  -- Get the link text as a list of inlines
  local ct = link.content
  local url = link.target
  -- Return just the link text if the link is local
  if url:match('^%#') then
    return ct
  end
  -- Get the link text as a string (without formatting)
  local txt = u.stringify(link)
  -- See if we got one of Pandoc's automatic classes
  for _, cls in ipairs(plain_classes) do
    if link.classes:includes(cls) then
      -- If yes return just `<URL>` or `<EMAIL>`
      return p.Str("<" .. tostring(bare_url(txt)) .. ">")
    end
  end
  -- Else if the link text contains the URL/email
  -- return just the link text
  if txt:match(quote_pat(bare_url(url))) then
    return ct
  end
  -- Else append `<URL>` or `<EMAIL>` to the link text
  ct:extend({ p.Space(), p.Str("<" .. tostring(bare_url(url)) .. ">") })
  return ct
end

[-- Attachment #4: result.md --]
[-- Type: text/markdown, Size: 234 bytes --]

Go to our site \<http://example.org>.

You can find us at http://example.org.

Go to our site at \<http://example.org>.

Drop us a mail \<info\@example.org>.

Send a mail to \<info\@example.org>.

Send a mail to \<info\@example.org>.

[-- Attachment #5: result.txt --]
[-- Type: text/plain, Size: 226 bytes --]

Go to our site <http://example.org>.

You can find us at http://example.org.

Go to our site at <http://example.org>.

Drop us a mail <info@example.org>.

Send a mail to <info@example.org>.

Send a mail to <info@example.org>.

[-- Attachment #6: test.md --]
[-- Type: text/markdown, Size: 397 bytes --]

Go to [our site](http://example.org).

[You can find us at http://example.org](http://example.org).

Go to our site at <http://example.org>.

Drop us a [mail](mailto:info-hcDgGtZH8xNAfugRpC6u6w@public.gmane.org).

Send a mail to <info-hcDgGtZH8xNAfugRpC6u6w@public.gmane.org>.

[Send a mail to <info-hcDgGtZH8xNAfugRpC6u6w@public.gmane.org>](mailto:info-hcDgGtZH8xNAfugRpC6u6w@public.gmane.org).


[-- Attachment #7: unfiltered-plain.txt --]
[-- Type: text/plain, Size: 226 bytes --]

Go to our site <http://example.org>.

You can find us at http://example.org.

Go to our site at <http://example.org>.

Drop us a mail <info@example.org>.

Send a mail to <info@example.org>.

Send a mail to <info@example.org>.

      parent reply	other threads:[~2022-01-15 14:44 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-14 11:20 Peter Mueller
     [not found] ` <f3cf23c8-71a0-4825-812d-652b8d1ccc71n-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
2022-01-15 14:41   ` BPJ
     [not found]     ` <c26e9460-27b6-ed81-2701-e1f3dcbac293-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2022-01-15 14:44       ` BPJ [this message]

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=90b5b9c0-3597-372e-5436-99a6f0c41d2f@gmail.com \
    --to=melroch-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).