markdown_links.rb 510 Bytes
Newer Older
Sylvester Keil's avatar
Sylvester Keil committed
1
# Contributed by @mfenner
Sylvester Keil's avatar
Sylvester Keil committed
2 3 4 5
# See https://github.com/inukshuk/jekyll-scholar/issues/30

require 'uri'

Sylvester Keil's avatar
Sylvester Keil committed
6 7 8 9 10
URL_PATTERN = Regexp.compile([
  '\\\\href\\\\{([^\\\\}]+)\\\\}\\\\{([^\\\\}]+)\\\\}',
  URI.regexp(['http', 'https', 'ftp'])
].join('|'))

Sylvester Keil's avatar
Sylvester Keil committed
11 12 13 14
module Jekyll
  class Scholar
    class Markdown < BibTeX::Filter
      def apply(value)
Sylvester Keil's avatar
Sylvester Keil committed
15 16 17 18 19 20 21
        value.to_s.gsub(URL_PATTERN) {
          if $1
            "[#{$2}](#{$1})"
          else
            "[#{$&}](#{$&})"
          end
        }
Sylvester Keil's avatar
Sylvester Keil committed
22 23 24 25
      end
    end
  end
end