Render url in reference

Created on 3 January 2017, over 8 years ago
Updated 15 December 2020, over 4 years ago

I have question about possibility to render the url in a reference as an actual url. Url:s are rendered as (example):
<span class="citeproc-URL">http://journals.lub.lu.se/index.php/pfs/article/view/7762</span>
Have I missed something here or is this an issue I have to solve in another way outside this module?

πŸ’¬ Support request
Status

Postponed

Version

1.0

Component

Code

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • Fully agree with lolcode (#5). Any plans on this?

  • πŸ‡ΊπŸ‡ΈUnited States mark_fullmer Tucson
  • πŸ‡ΊπŸ‡ΈUnited States mark_fullmer Tucson

    Individual sites could also potentially rewrite such URLs as actual hyperlinks by implementing HOOK__preprocess_bibcite_citation(), which provides access to the already-rendered citation. Presumably you could use regular expressions to find/replace specific markup with hyperlinks. This would not be as elegant a solution as using Lambda functions to modify the citation processor itself, as described in https://github.com/seboettg/citeproc-php#use-lambda-functions-to-setup-c... , but might be more straightforward for some developers.

  • Thanks. Yes, that should be possible but because I also want to put the title into a span, I would have to rerender the whole citation in HOOK__preprocess_bibcite_citation().
    This can be avoided by overriding the BibCiteProcessor plugin (citeproc-php) with HOOK_bibcite_bibcite_processor_info_alter() and passing the lambda functions to CiteProc() in render() there.

  • πŸ‡ΊπŸ‡ΈUnited States mark_fullmer Tucson

    Okay, changing this to a "Support request." Let's try to add some documentation that shows example usage of both HOOK__preprocess_bibcite_citation() and HOOK_bibcite_bibcite_processor_info_alter()

  • This is the first time I am posting; unsure how to proceed. If appropriate I can supply some code snippets related to the info_alter option.

  • πŸ‡ΊπŸ‡ΈUnited States mark_fullmer Tucson

    This is the first time I am posting; unsure how to proceed. If appropriate I can supply some code snippets related to the info_alter option.

    Code snippets in a comment would be a great start. I can then move them into a new page under the documentation at https://www.drupal.org/docs/contributed-modules/bibliography-citation β†’

  • In MYMODULE.module:

    /**
     * Overwrite the plugin Drupal\bibcite\Plugin\BibCiteProcessor\CiteprocPhp.
     * We want to be able to use the Lambda functions of seboettg/citeproc-php.
     * See \vendor\seboettg\citeproc-php\README.md.
     *
     * (If it is not clear whether this function gets called, flush
     * the plugins cache...)
     */
    function MYMODULE_bibcite_bibcite_processor_info_alter(array &$definitions) {
      $definitions['citeproc-php']['class'] =
      'Drupal\MYMODULE\Plugin\BibCiteProcessor\CiteprocPhpFOOBAR';
    }
    

    MYMODULE\src\Plugin\BibCiteProcessor\CiteprocPhpFOOBAR.php:

    <?php
    
    namespace Drupal\MYMODULE\Plugin\BibCiteProcessor;
    
    use Seboettg\CiteProc\CiteProc;
    use Drupal\bibcite\Plugin\BibCiteProcessor\CiteprocPhp;
    
    /**
     * Defines a style provider based on citeproc-php library.
     * This class extends the original class 
     * \vendor\seboettg\citeproc-php\src\CiteProc.php.
     *
     * @BibCiteProcessor(
     *   id = "citeproc-php-FOOBAR",
     *   label = @Translation("Citeproc PHP (FOOBAR)"),
     * )
     */
    class CiteprocPhpFOOBAR extends CiteprocPhp {
    
      /**
       * {@inheritdoc}
       *
       * WARNING: This function is an example only. Your mileage *will* vary
       * depending on several factors; e.g. the CSL you are using.
       */
      public function render($data, $csl, $lang) {
        //
        // The following are Lambda functions for seboettg/citeproc-php.
        // See \vendor\seboettg\citeproc-php\README.md.
        $title_function = function($cslItem, $renderedText) {
          //
          // Add some classes to the title
          $title = '<span class="citeproc-title-and-descriptions">';
          $title .= '<span class="citeproc-title">';
          $title .= $renderedText;
          $title .= '</span></span>';
          return $title;
        };
        $doi_function = function($cslItem, $renderedText) {
          //
          // Pack the URL in an anchor
          //
          // WARNING: The situation here is quite complicated
          // and what can be done here probably depends on the
          // active CSL and on the contents of the DOI field 
          // in your bibcite records.
          // (See also the comment below.)
          $doi_link = '<span class="citeproc-access">';
          $doi_link .= '<a href="';
          if (!str_starts_with($renderedText, 'http')) {
            $doi_link .= 'https://doi.org/';
          }
          $doi_link .= $renderedText . '" target="_blank">';
          $doi_link .= '(abstract)</a></span>';
          return $doi_link;
        };
        $additionalMarkup = [
          'title' => $title_function,
          'DOI' => $doi_function
        ];
        
        $cite_proc = new CiteProc($csl, $lang, $additionalMarkup);
        if (!$data instanceof \stdClass) {
          $data = json_decode(json_encode($data));
        }
        $res = preg_replace('/(\\n|\r)( *)/', '', $cite_proc->render([$data]));
        //
        // CSLs can contain prefixes and suffixes. They are added  
        // only *after* the lambda functions above have run. As an 
        // alternative to the code below we could of course craft 
        // our own CSLs...
        //
        // We have to remove the prefix ('https://doi.org/') of the 
        // access field here because it has been added *after* we have 
        // already added a span around our anchor tag in $doi_function.
        $res = str_replace('https://doi.org/<span class="citeproc-access">',
                           '<span class="citeproc-access">', $res);
        //
        // Remove some unwanted <i>-Tags
        $res = str_replace(array('<i>', '</i>'), '', $res);
        return $res;
      }
    }
    
  • πŸ‡ΊπŸ‡ΈUnited States mark_fullmer Tucson
  • πŸ‡ΊπŸ‡ΈUnited States mark_fullmer Tucson

    After thinking about this further, I think that this is a common, reasonable request that should not require a developer in order to accomplish. I therefore propose adding a new, global setting to Bibcite for "Convert URLs into hyperlinks".

    When enabled, this will pass the already-processed citation through Drupal core's _filter_url() function, which is robust and designed to handled a wide range of HTML content.

    This approach does not give sites the ability to have some URLs be hyperlinks and others not be hyperlinks -- that, I feel, still should be the purlieu of developers and custom code.

  • πŸ‡ΊπŸ‡ΈUnited States mark_fullmer Tucson
  • Pipeline finished with Skipped
    10 days ago
    #557176
  • Pipeline finished with Skipped
    10 days ago
    #557177
  • πŸ‡ΊπŸ‡ΈUnited States mark_fullmer Tucson

    As this is an opt-feature, I feel comfortable merging this in; should there need to be refinements, we can work those out subsequently.

Production build 0.71.5 2024