-
Notifications
You must be signed in to change notification settings - Fork 102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Turning URLs into links #30
Comments
Absolutely. Jekyll-Scholar started out as very quick plugin — the idea was that it should allow you to re-use the utilities and override selected methods if you need custom behaviour. So your solution is basically what I assumed people would do. Another cool solution I've seen is by @ashinkarov here #23 using a custom CSL style. When we finally finish the citeproc-ruby rewrite it will be much easier to hook into the cite processing and adapt individual components. As far as adding such customizations to the jekyll-scholar gem I'm not sure if it is really worth it to support this functionality and not better to make it easy for people to override everything to their use case. The big problem here is that I'm probably the least experienced jekyll-scholar user myself. My rule of thumb is that I'll gladly add anything that is generic enough to be useful to most users as long as it is not difficult to maintain, so if you turn this into a pull request and add a test to make maintenance easier I'll gladly merge it :-) |
I think that the problem is that the proposed solution is not generic enough. For example on my page, I put "[url]" text that leads to the paper I am referring to. Assuming your patch would be merged-in, my publication list would be broken. I believe it is arbitrary hard to hard-code this sort of things to make everyone happy. So as I wrote earlier, the solution that we really need is a dynamic modification of csl files. In essence it's all about representation of the data -- we are not changing the essence of the data. |
I am confused, or maybe I don't understand CSL enough. What I am thinking I propose is that every time a URL is written to the markdown file we are generating, we add standard markdown tags for a link around it. Why should this conflict with other URLs in the CSL? And is there a case for URLs in citations to not be links? |
CSL is just a template which is filled with the data from each individual bibitem. For example the content of the title may be taken in pads, or italic might be applied or something else. When it comes to URLs, if not being ignored, then they are passed as clear text, or adding "url:" prefix (it all depends on the style we are using). Now you are proposing to preprocess url content and instead of clear text, you want to give Now, my problem here would be, that I don't want to see If you do this processing step at the level of CSL, then you don't have to modify the data that is comming to this CSL. Now, if you hardcode these things, you will need to find a way to parametrise your code, which might or might not cover all the cases. So, the generic solution would be to specify xml code snippets in your config.yml (or somewhere else), which would add/replace parts of XML tree of the CSL files. I hope that I explained it well enough. Feel free to ask more. |
In general I tend to agree with @ashinkarov here that reference data should be kept clean of any sort of markup (html, markdown, latex or otherwise); however, in this case this is completely internal to jekyll-scholar and jekyll-scholar as a blogging tool obviously generates html via markdown. So I think the proposed solution is a pretty neat way of turning the URLs into proper links by default. Actually, I have thought about this a little and we may even accomplish this as a filter in bibtex-ruby; we could then simply add the name of the filter to the jekyll-scholar configuration to preprocess URLs and DOIs in this way. This way you wouldn't even need to override @mfenner do you see any problem if the conversion is applied 'permanently' (of course only to the bibliography in memory not on disk) instead of every time in reference tag? Then we could just add a filter to handle the conversion after the bibliography is parsed. |
@ashinkarov your arguments make sense. I am using plain CSL styles without modifications so wasn't considering custom formatting. @inukshuk a filter in bibtex-ruby would be fine. I can also continue monkey patching jekyll-scholar. |
@mfenner if you like you could you try something like this. Put a file markdown.rb (file name is not relevant) into your ext directory. There you need to create a class
And then in your jekyll configuration set:
This would turn all values (not only in the URL field) into a markdown link permanently as far as jekyll scholar is concerned. If this works I think we could add this filter to jekyll-scholar by default for you to enable with the bibtex_filters options on demand. |
@inukshuk My require 'jekyll/scholar'
require 'uri'
module MarkdownFilter
class Markdown < BibTeX::Filter
def apply(value)
url = value.to_s.slice(URI.regexp(['http','https','ftp']))
value = url ? "[#{url}](#{url})" : value
end
end
end This works and is now live on my jekyll blog. I have two small issues:
Auto-linking with Github-flavored markdown is also broken for this URL. |
Nice! I did not know about The issue I see is that, in theory, you could have something like this (in the
That's why I would rather replace the URLs with As for the second issue, perhaps we can add a separate pattern for DOIs; something like |
OK, I changed the code to do replace the URL, even if part of the field value: require 'jekyll/scholar'
require 'uri'
module MarkdownFilter
class Markdown < BibTeX::Filter
def apply(value)
url = value.to_s.slice(URI.regexp(['http','https','ftp']))
return value unless url
value.to_s.gsub(/(#{url})/, '[\1](\1)')
end
end
end URI.regexp currently doesn't find some URLs, e.g. those that contain parentheses (because they confuse the regex matching) or really strange things like the URL above. Too bad, but least they are displayed as text. Will look at other patterns for URL matching. |
OK, now I have the bibtex filter working the way I want: require 'jekyll/scholar'
require 'uri'
module MarkdownFilter
class Markdown < BibTeX::Filter
def apply(value)
value.to_s.gsub(URI.regexp(['http','https','ftp'])) { |c| "[#{$&}](#{$&})" }
end
end
end This code is pretty dense, but I didn't want to regex twice. This only works with valid URLs, but I see that as a feature, not a bug. The problem of DOIs in funky formats I solved differently as I produced the URL myself: I changed the code to generate a URL from a DOI: url = Addressable::URI.escape "http://dx.doi.org/#{doi}" instead of url = "http://dx.doi.org/#{doi}" URI.escape is depreciated and CGI.escape does something slightly different, therefore the addressable gem. |
This looks pretty good! I think it would be great to add the filter to jekyll-scholar — the only question is whether or not we should enable it by default. |
That looks good, assuming that URI.regexp works fine on various links :) Cheers, |
Yes, the nice thing is that @mfenner turned it into a BibTeX-Ruby filter so we already have the option in place: |
Ah, ok, I didn't remember that we have bibtex_filter available via configuration. |
@inukshuk will you add this filter to bibtex-ruby or jekyll-scholar? It would be good to add Cucumber tests to confirm that this filter can handle a variety of links. I would be happy to write the tests, but not before the weekend. I'm for enabling by default since jekyll-scholar is always using markdown. |
@mfenner cucumber tests would be great. I was thinking of adding it to jekyll-scholar and I also think it would be a reasonable default — we just need to make sure people know how to switch to the old configuration. |
OK, give me a few days and I will try a pull request that includes cucumber tests. Unless you want to add the filter yourself, then the pull request will only contain the tests. |
No hurry : ) just let me know if I can do anything to help. |
I tried this and it just shows the markup in the bibliography list. I have:
which shows as Do I need to configure something else? |
I think we never actually added the filter, so what you need to do is add it to your
And then enable it in
|
yes, that is what I did |
@mfenner is it OK if I add the plugin? Looks like I should write a test for it anyway : ) |
I think for this to work the way @mfenner intended this needs to happen inside a markdown file, because you need the markdown processor to then convert the link. Alternatively, you could adjust the filter above to generate an HTML link tag instead. Something like:
Better yet, you could adjust your bibliography template |
@inukshuk fee free to add the plugin. |
Changing the filter to generate a HTML link tag works. Thanks. |
this should turn them into links, according to inukshuk/jekyll-scholar#30 (comment) but it doesn't. bummer.
Hmm, it doesn't seem to be working this way for me. I have modified my If you're interested, here's the CSL style, my config file and the resulting output. |
The angle brackets are inserted into your markdown documents as HTML entities |
Hey, thanks for taking the time to help out. I tried this in the
and it renders out like (e.g.) this in the final
so it doesn't seem to be getting markdownified. The "using Or maybe I'm just doing it wrong. |
This reverts commit c85f39b. it doesn't work this way anyway - see inukshuk/jekyll-scholar#30
OK I just tried this using the markdown filter above and I noticed two things: first, if you're using the |
Yep, I tried It's not a dealbreaker---people can copy-paste it into their url bar 😉 So I think I'll park it for now. Thanks for the work you did to investigate. |
If it's helpful for others who come across this, my blog source is up at https://github.com/benswift/benswift.github.io. And here's the gory details about all the gem versions from
|
hi @benswift , at least on your research page the urls ar not clickable for me, so please tell me exactly where to look to solve the problem of this thread. Thanks very much! |
I didn’t solve it, sadly. And I tried all the suggestions, so I’m not 100% where to go from here. Sorry. |
Has anybody with Jekyll experience figured out how to get the output of the liquid tag to be processed by the markdown engine? For the moment -> the output of |
Like others the markdown link plugin is not working for me, even though the order of operations documentation implies it should be fine. However, html output is fine, so it wasn't too hard to modify that plugin to generate links that way. The following does what's needed in jekyll 4.0.0 (you can put this as a .rb file in the # Contributed by @mfenner as markdown_links.rb
# See https://github.com/inukshuk/jekyll-scholar/issues/30
# modified to generate html instead of markdown by @rawlins
require 'uri'
module Jekyll
class Scholar
class HtmlLinks < BibTeX::Filter
URL_PATTERN = URI.regexp(['http', 'https', 'ftp'])
def apply(value)
value.to_s.gsub(URL_PATTERN) {
"<a href=\"#{$&}\">#{$&}</a>"
}
end
end
end
end Does not handle DOIs for the ordering reasons described above, I'm still trying to figure out what would be needed for them for my own purposes. |
Ok, here's a solution for DOIs that works for me, though it has some limitations. First, if you are using a CSL that inserts URL material into the DOI field, you will need to disable that. For example, the one I'm using had a conditional: <if variable="DOI">
<text variable="DOI" prefix="DOI: https://doi.org/" /> -->
</if> so I made a copy of this CSL file in my site directory structure, changed the config so it was using this copy, changed this rule to to: <if variable="DOI">
<text variable="DOI" prefix="DOI: " />
</if> Second, I installed the following plugin into my site's # Convert DOIs to html. This *will not match all DOIs*. The regex comes from
# https://www.crossref.org/blog/dois-and-matching-regular-expressions/
# this may conflict with some CSLs that insert `https://doi.org/` or the like.
module Jekyll
class Scholar
class DOILinks < BibTeX::Filter
DOI_FILTER = Regexp.new("^10\\.\\d{4,9}/[-._;()/:A-Z0-9]+$", Regexp::IGNORECASE)
def apply(value)
value.to_s.gsub(DOI_FILTER) {
"<a href=\"https://doi.org/#{$&}\">#{$&}</a>"
}
end
end
end
end As noted, this won't match exotic DOIs (see the link in the code comments above for discussion). For purposes of bibtex value strings, I actually suspect that something much more relaxed like |
The approach mentioned by @benswift did not work for me either, but I think I found a way to go around this issue by writing a custom Liquid filter: # put inside file: _plugin/link_to.rb
module Jekyll::CustomFilter
def link_to(input, href)
"<a href=\"#{href}\">#{input}</a>"
end
end
Liquid::Template.register_filter(Jekyll::CustomFilter) Then the <if variable="URL">
<text variable="title" quotes="true" prefix="{{ '" suffix="' | "/>
<text variable="URL" prefix="link_to: '" suffix="' }}"/>
</if>
<else>
<text variable="title" quotes="true"/>
</else> It seems to work, but it feels more like a horrible workaround rather than a proper solution. |
@m-pilia have you tried this in the latest version of jekyll-scholar? I think this will not work anymore, because we just disabled the double parsing of templates. I think the best option is to use a custom bibliography template; this gives you full access to the entry and to the rendered reference. If you absolutely need the link to be inside the rendered reference, I think the best solution right now is to hack the style, similar to your own example. You can use this code to make it possible to insert HTML snippets into the prefix/suffix attributes (using either '<' or '<' should work). (We should probably add something like this to jekyll-scholar behind an option.) |
@inukshuk Thanks for the reply! Indeed, I tried and I can confirm that using Liquid filters inside the CSL does not work on the latetst version. Your suggestion works better though. |
Has a solution to this ever been made 'standard', or are we still stuck with adding custom hacks? Latest jekyll-scholar, with default (apa) style, still fails on @x12a1f 's simple test case:
Still produces the bibliography entry that reads...
which is never converted to HTML. (i.e. no links). ...this is even with
Turning on markdown filter or not doing so, seems to have no effect. Only solution I can find is to remove the markdown filter and then manually edit the BibTeX to include the HTML:
...not ideal. |
I modified the markdown filter to produce the proper HTML, quattrinili@de8cc5d I am still testing it to see if that works in general, and probably the right way to do it is to change the name of that filter, but at least it is producing the correct output. |
I would like to know the steps required to make the title text point the doi. All my bibtex entries have the doi field and I would like to use them. I updated my custom csl file as follows
And my _config.yml has the latex and markdown filters enabled.
I am using the minima theme and kramdown. I tried the html tags Any suggestions would be useful. I am using jekyll 4.2.0 and jekyll-scholar (6.8.0). |
Is there something that needs to be done to get the custom ruby files in I've been following the instructions of @m-pilia in this blog. At the moment I see the HTML string rather than it being interpreted by the browser.
|
All Ruby files in Regarding the link, I guess the solution mentioned in the blog is to downgrade to a previous version of jekyll-scholar. In general, I'd try to remove the DOI from the CSL style and use a custom bibliography template to display the rendered reference and then add your own links for URLs, DOIs and similar. |
Thanks, I ended up using the template approach. I hadn't picked up on that in the README. It would be handy to give the set of keys that are available or a pointer to a page that gives them. |
I was stuck with this, too. Here is a hack that worked at least for my situation:
with
and now the links are rendered correctly. |
I had the same issue and built this hopefully elegant enough solution (no plugin) _bibliography/references.bib @article{test,
author = {test},
title = {test},
hidden_url = {http://google.com}
} _config.yml scholar:
bibliography_template: bib _layouts/bib.html ---
---
{% if entry.hidden_url %}
<a href="{{ entry.hidden_url }}">{{reference}}</a>
{% else %} {{reference}} {% endif %} |
I want URLs in citations to be links, and I have monkey patched the
reference_tag
method to allow this:You can see that I change
entry["url"]
into a markdown link, which is then picked up my the markdown processor. You can see this at work at http://blog.martinfenner.org/about.html. Is this the correct way of doing this? I'm using Pandoc as markdown processor and in the default configuration it doesn't recognize blank URLs.We could do something similar for
entry["doi"]
, as some styles display the DOI.The text was updated successfully, but these errors were encountered: