-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjbibhtml.php
More file actions
130 lines (114 loc) · 4.1 KB
/
Copy pathjbibhtml.php
File metadata and controls
130 lines (114 loc) · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
/**
* @package jbibhtml
* @subpackage com_jbibhtml
*
* @license GNU General Public License version 2 or later
*/
// No direct access
defined('_JEXEC') or die;
/**
* Jbibhtml Content Plugin
*/
class plgContentJbibhtml extends JPlugin
{
/**
* Method to parse $article->text string and insert bibliography html.
*
* Method is called by the view
*
* @param string The context of the content being passed to the plugin.
* @param object The content object. Note $article->text is also available
* @param object The content params
* @param int The 'page' number
*
* @return boolean True on success.
*/
public function onContentPrepare($context, &$article, &$params, $limitstart)
{
//Do some quick checks to determine if we should process further.
//Possible contexts: com_content.featured, com_content.category, com_content.article
$parts = explode(".", $context);
if ($parts[0] != 'com_content')
{
return true;
}
// Also add a quick check to see if we should process further.
//Note this is in complete in contexts like com_content.category where $article->text is the introtext.
if(JString::strpos($article->text, '{jbibhtml') === false) {
return true;
}
$bibFile = $this->params->get('bibtex_file', '');
if(!preg_match("/{jbibhtml\s*(bibFile=\"([^\"]*)\")?\s*}/i", $article->text, $jbibhtml)) {
return true;
} elseif(count($jbibhtml) == 3) {
//Custom bibFile was given.
$bibFile = $jbibhtml[2];
}
// Return if we don't have a bibtex_file.
if (empty($bibFile))
{
JError::raiseWarning( 100, "JBibHtml Error: Bibtex file not given." );
return true;
}
//Load bibtexbrowser as a library
$_GET['library']=1;
define('BIBTEXBROWSER_BIBTEX_LINKS',false);
define('BIBTEXBROWSER_PDF_LINKS',false);
define('ABBRV_TYPE', $this->params->get('ABBRV_TYPE', 'index'));
require_once JPATH_SITE . '/plugins/content/jbibhtml/bibtexbrowser.php';
global $db;
$db = new BibDataBase();
$db->load(JPATH_SITE . $bibFile);
//Find all occurences like \cite{Smith2003} or \cite[theorem 5]{Smith2003} in $article->text.
$result_count = preg_match_all("/\\\cite(\[([^\]]*)\])?\{([^\}]*)\}/", $article->text, $pat_array);
$resKeys = array();
//First loop through matches and fill in $resKeys.
for ($j = 0; $j < count($pat_array[0]); $j++) {
$match = $pat_array[3][$j];
$bibKeys = explode(",", $match);
for ($i = 0; $i < count($bibKeys); $i++) {
if ($db->contains($bibKeys[$i])) {
$resKeys[] = $bibKeys[$i];
}
}
}
//We use our own custom method, ArticleDisplay. Could also use SimpleDisplay or AcademicDisplay.
//TODO: how are the entries being sorted?
$d = new ArticleDisplay();
//Heading level is decreased so that the ArticleDisplay->display function doesn't output total count.
$d->decHeadingLevel();
//Filter by bibtex keys, $resKeys$, cited in the article.
$entries = $db->multisearch(array('keys'=> $resKeys));
$d->setEntries($entries);
//Load all HTML output into a string.
ob_start();
$d->display();
$content = ob_get_contents();
ob_end_clean();
$article->text = str_replace($jbibhtml[0], $content, $article->text);
//In this loop, we create links such as [Smith2003] which replaces \cite{Smith2003} in $article->text.
//It also handles the case we have something like \cite[theorem 5]{Smith2003}.
for ($j = 0; $j < count($pat_array[0]); $j++) {
$match = $pat_array[3][$j];
$bibKeys = explode(",", $match);
$link = "[";
for ($i = 0; $i < count($bibKeys); $i++) {
if (!$db->contains($bibKeys[$i])) {
JError::raiseWarning( 100, "JBibHtml Error: Bibtex key '$bibKeys[$i]' not found in file '$bibFile'." );
} else {
$entry = $db->getEntryByKey($bibKeys[$i]);
if($i > 0) $link .= "; ";
$link .= '<a href="' . JURI::current(). '#' . $entry->getRawAbbrv() . '"><span class="bibkey">' . $entry->getRawAbbrv() . '</span></a>';
}
}
if($pat_array[2][$j] != "") {
$link .= ", " . $pat_array[2][$j];
}
$link .= "]";
//Link to the bibtex entry.
$article->text = str_replace($pat_array[0][$j], $link, $article->text);
}
return true;
}
}