From 07f3f833ea4202e07d54af576b96cd701a435748 Mon Sep 17 00:00:00 2001 From: Alexander O'Neill Date: Thu, 28 Jan 2021 21:25:06 -0400 Subject: [PATCH] Remove dependency on Footnotes module. --- bibcite_footnotes.info.yml | 4 +- composer.json | 23 +++++ css/reference_footnote.css | 87 +++++++++++++++++- .../Filter/ReferenceFootnotesFilter.php | 92 ++++++++++++++++++- 4 files changed, 202 insertions(+), 4 deletions(-) create mode 100644 composer.json diff --git a/bibcite_footnotes.info.yml b/bibcite_footnotes.info.yml index f017b05..43b44a9 100644 --- a/bibcite_footnotes.info.yml +++ b/bibcite_footnotes.info.yml @@ -7,4 +7,6 @@ dependencies: - bibcite:bibcite - bibcite:bibcite_entity - entity_reference - - footnotes:footnotes + - fakeobjects:fakeobjects + + diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..f1a63c3 --- /dev/null +++ b/composer.json @@ -0,0 +1,23 @@ +{ + "name": "drupal/bibcite_footnotes", + "description": "Add citation footnotes with automatic numbering.", + "type": "drupal-module", + "homepage": "https://drupal.org/project/bibcite_footnotes", + "authors": [ + { + "name": "Alexander O'Neill (alxp)", + "homepage": "https://www.drupal.org/u/alxp", + "role": "Maintainer" + } + ], + "support": { + "issues": "https://drupal.org/project/issues/bibcite_footnotes", + "source": "https://cgit.drupalcode.org/bibcite_footnotes" + }, + "license": "GPL-2.0+", + "minimum-stability": "dev", + "require": { + "drupal/core": ">=8.6", + "drupal/fakeobjects": "^1.0" + } +} diff --git a/css/reference_footnote.css b/css/reference_footnote.css index 2529217..8c473d9 100644 --- a/css/reference_footnote.css +++ b/css/reference_footnote.css @@ -7,4 +7,89 @@ .footnote-list .footnotes .footnote-multi::after { content: " "; white-space: pre; -} \ No newline at end of file +} + +/* + * CSS specific to Bibcite Footnotes module. + * (Borrowed from Footnotes module. + * +* +* Thanks to binford2k@lug.wsu.edu for this tip and drinkypoo +* for the question leading up to it. http://drupal.org/node/80538 +*/ + +/* Add empty space before footnotes and a black line on top. */ +.footnotes { + clear: both; + margin-top: 4em; + margin-bottom: 2em; + border-top: 1px solid #000; +} +/* Make footnotes appear in a smaller font */ +.footnotes { + font-size: 0.9em; +} +/* + Make the footnote a supertext^1 +*/ +.see-footnote { + vertical-align: top; + position: relative; + top: -0.25em; + font-size: 0.9em; +} +/* Hide the bullet of the UL list of footnotes */ + +ul.footnotes { + list-style-type: none; + margin-left: 0; + padding-left: 0; +} +ul.footnotes li { + margin-left: 2.5em; + list-style-type: none; + background: none; /* Garland theme sets a bullet via background image, this must be unset! See bug 861634 */ +} +/* Move the footnote number outside of the margin for footnote text (hanging indent) */ +ul.footnotes { + /* This is apparently very needed for the "position: absolute;" below to work correctly */ + position: relative; +} +.footnotes .footnote-label { + position: absolute; + left: 0; + z-index: 2; +} +/* Highlight the target footnote (or ref number, if a backlink was used) when user clicks a footnote. */ +.see-footnote:target, +.footnotes .footnote:target { + background-color: #eee; +} +.see-footnote:target { + border: solid 1px #aaa; +} +/* Note: This CSS has a minor bug on all versions of IE in that the footnote numbers +are aligned with the absolute bottom of their space, thus being a couple of pixels +lower than their corresponding line of text. IE5.5 has a serious bug in that the numbers +are not shifted left at all, thus being garbled together with the start of their text. */ + +/* + Make the multiple backlinks a supertext^1 +*/ +.footnotes .footnote-multi { + vertical-align: top; + position: relative; + top: -0.25em; + font-size: 0.75em; +} +/* + * Textile Footnotes + */ +/* First footnote */ +#fn1 { + border-top: 1px solid #000; + margin-top: 3em; +} +.footnote { + font-size: 0.9em; +} diff --git a/src/Plugin/Filter/ReferenceFootnotesFilter.php b/src/Plugin/Filter/ReferenceFootnotesFilter.php index 6890717..639ecd0 100644 --- a/src/Plugin/Filter/ReferenceFootnotesFilter.php +++ b/src/Plugin/Filter/ReferenceFootnotesFilter.php @@ -4,7 +4,9 @@ namespace Drupal\bibcite_footnotes\Plugin\Filter; use Drupal\Component\Utility\Xss; use Drupal\Core\Form\FormStateInterface; -use Drupal\footnotes\Plugin\Filter\FootnotesFilter; +use Drupal\filter\FilterProcessResult; +use Drupal\filter\Plugin\FilterBase; + /** * Reference Footnotes filter. @@ -24,7 +26,7 @@ use Drupal\footnotes\Plugin\Filter\FootnotesFilter; * weight = 0 * ) */ -class ReferenceFootnotesFilter extends FootnotesFilter { +class ReferenceFootnotesFilter extends FilterBase { /** * Object with configuration for reference footnotes. @@ -46,6 +48,7 @@ class ReferenceFootnotesFilter extends FootnotesFilter { public function __construct(array $configuration, $plugin_id, array $plugin_definition) { parent::__construct($configuration, $plugin_id, $plugin_definition); + $this->renderer = \Drupal::service('renderer'); $this->config = \Drupal::config('reference_footnotes.settings'); $this->configEditable = \Drupal::configFactory() ->getEditable('reference_footnotes.settings'); @@ -310,4 +313,89 @@ class ReferenceFootnotesFilter extends FootnotesFilter { $prev_reference_id = $fn['reference']; } } + + /** + * {@inheritdoc} + */ + public function process($text, $langcode) { + // Supporting both [fn] and now. Thanks to fletchgqc + // http://drupal.org/node/268026. + // Convert all square brackets to angle brackets. This way all further code + // just manipulates angle brackets. (Angle brackets are preferred here for + // the simple reason that square brackets are more tedious to use in + // regexps). + if (is_array($text)) { + implode($text); + } + $text = preg_replace('|\[fn([^\]]*)\]|', '', $text); + $text = preg_replace('|\[/fn\]|', '', $text); + $text = preg_replace('|\[footnotes([^\]]*)\]|', '', $text); + + // Check that there are an even number of open and closing tags. + // If there is one closing tag missing, append this to the end. + // If there is more disparity, throw a warning and continue. + // A closing tag may sometimes be missing when we are processing a teaser + // and it has been cut in the middle of the footnote. + // See http://drupal.org/node/253326 + $foo = []; + $open_tags = preg_match_all("|]*)>|", $text, $foo); + $close_tags = preg_match_all("||", $text, $foo); + + if ($open_tags == $close_tags + 1) { + $text = $text . ''; + } + elseif ($open_tags > $close_tags + 1) { + trigger_error($this->t("You have unclosed fn tags. This is invalid and will + produce unpredictable results.")); + } + + // Before doing the replacement, the callback function needs to know which + // options to use. + $this->replaceCallback($this->settings, 'prepare'); + + $pattern = '|]*)>(.*?)|s'; + $text = preg_replace_callback($pattern, [ + $this, + 'replaceCallback', + ], $text); + + // Replace tag with the list of footnotes. + // If tag is not present, by default add the footnotes at the end. + // Thanks to acp on drupal.org for this idea. see + // http://drupal.org/node/87226. + $footer = $this->replaceCallback(NULL, 'output footer'); + $pattern = '|()|'; + if (preg_match($pattern, $text) > 0) { + $text = preg_replace($pattern, $footer, $text, 1); + } + elseif (!empty($footer)) { + $text .= "\n\n" . $footer; + } + $result = new FilterProcessResult($text); + $result->setAttachments([ + 'library' => [ + 'footnotes/footnotes', + ], + ]); + return $result; + } + + + /** + * Helper function to return a random text string. + * + * @return string + * Random (lowercase) alphanumeric string. + */ + public function randstr() { + $chars = "abcdefghijklmnopqrstuwxyz1234567890"; + $str = ""; + + // Seeding with srand() not necessary in modern PHP versions. + for ($i = 0; $i < 7; $i++) { + $n = rand(0, strlen($chars) - 1); + $str .= substr($chars, $n, 1); + } + return $str; + } }