Browse Source

Remove dependency on Footnotes module.

8.x-1.x
Alexander O'Neill 4 years ago
parent
commit
07f3f833ea
  1. 4
      bibcite_footnotes.info.yml
  2. 23
      composer.json
  3. 85
      css/reference_footnote.css
  4. 92
      src/Plugin/Filter/ReferenceFootnotesFilter.php

4
bibcite_footnotes.info.yml

@ -7,4 +7,6 @@ dependencies:
- bibcite:bibcite - bibcite:bibcite
- bibcite:bibcite_entity - bibcite:bibcite_entity
- entity_reference - entity_reference
- footnotes:footnotes - fakeobjects:fakeobjects

23
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"
}
}

85
css/reference_footnote.css

@ -8,3 +8,88 @@
content: " "; content: " ";
white-space: pre; white-space: pre;
} }
/*
* 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;
}

92
src/Plugin/Filter/ReferenceFootnotesFilter.php

@ -4,7 +4,9 @@ namespace Drupal\bibcite_footnotes\Plugin\Filter;
use Drupal\Component\Utility\Xss; use Drupal\Component\Utility\Xss;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
use Drupal\footnotes\Plugin\Filter\FootnotesFilter; use Drupal\filter\FilterProcessResult;
use Drupal\filter\Plugin\FilterBase;
/** /**
* Reference Footnotes filter. * Reference Footnotes filter.
@ -24,7 +26,7 @@ use Drupal\footnotes\Plugin\Filter\FootnotesFilter;
* weight = 0 * weight = 0
* ) * )
*/ */
class ReferenceFootnotesFilter extends FootnotesFilter { class ReferenceFootnotesFilter extends FilterBase {
/** /**
* Object with configuration for reference footnotes. * Object with configuration for reference footnotes.
@ -46,6 +48,7 @@ class ReferenceFootnotesFilter extends FootnotesFilter {
public function __construct(array $configuration, $plugin_id, array $plugin_definition) { public function __construct(array $configuration, $plugin_id, array $plugin_definition) {
parent::__construct($configuration, $plugin_id, $plugin_definition); parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->renderer = \Drupal::service('renderer');
$this->config = \Drupal::config('reference_footnotes.settings'); $this->config = \Drupal::config('reference_footnotes.settings');
$this->configEditable = \Drupal::configFactory() $this->configEditable = \Drupal::configFactory()
->getEditable('reference_footnotes.settings'); ->getEditable('reference_footnotes.settings');
@ -310,4 +313,89 @@ class ReferenceFootnotesFilter extends FootnotesFilter {
$prev_reference_id = $fn['reference']; $prev_reference_id = $fn['reference'];
} }
} }
/**
* {@inheritdoc}
*/
public function process($text, $langcode) {
// Supporting both [fn] and <fn> 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([^\]]*)\]|', '<fn$1>', $text);
$text = preg_replace('|\[/fn\]|', '</fn>', $text);
$text = preg_replace('|\[footnotes([^\]]*)\]|', '<footnotes$1>', $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("|<fn([^>]*)>|", $text, $foo);
$close_tags = preg_match_all("|</fn>|", $text, $foo);
if ($open_tags == $close_tags + 1) {
$text = $text . '</fn>';
}
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 = '|<fn([^>]*)>(.*?)</fn>|s';
$text = preg_replace_callback($pattern, [
$this,
'replaceCallback',
], $text);
// Replace tag <footnotes> 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 = '|(<footnotes([^\]]*)>)|';
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;
}
} }

Loading…
Cancel
Save