Footnotes

Third Party Plugins to CMSimple - how to install, use and create plugins

Moderator: Tata

Post Reply
cmb
Posts: 14225
Joined: Tue Jun 21, 2011 11:04 am
Location: Bingen, RLP, DE
Contact:

Footnotes

Post by cmb » Tue Mar 03, 2015 1:12 pm

Hello Community,

I just received an email with the idea to have a simple syntax to add footnotes similar to MediaWiki and DokuWiki. The idea is to just write [[footnote text]] somewhere in the page content, and let CMSimple_XH turn that into a proper footnote. The following addition to cmsimple/userfuncs.php is supposed to do the trick:

Code: Select all

function convertFootnotes()
{
    global $s, $c, $su;

    $i = $s > -1 ? $s : 0;
    $count = preg_match_all(
        '/\[\[(.*)\]\]/U', $c[$i], $matches, PREG_SET_ORDER
    );
    if ($count) {
        $c[$i] .= '<div class="footnotes">';
        foreach ($matches as $j => $match) {
            $c[$i] = str_replace(
                $match[0],
                '<sup><a href="#footnote' . ($j + 1)
                . '">[' . ($j + 1) . ']</a></sup>',
                $c[$i]
            );
            $c[$i] .= '<div id="footnote' . ($j + 1) . '">'
                . ($j + 1) . '. ' . $match[1] . '</div>';
        }
        $c[$i] .= '</div>';
    }
}

if (!(XH_ADM && $edit)) {
    convertFootnotes();
} 
Enjoy!

PS: slightly edited for ungreedy matching, see below for an explanation.

PPS: Enclosed all footnotes in <div class="footnote">; see below.
Christoph M. Becker – Plugins for CMSimple_XH

svasti
Posts: 1651
Joined: Wed Dec 17, 2008 5:08 pm

Re: Footnotes

Post by svasti » Tue Mar 03, 2015 7:46 pm

It seems I got the same request... :?
However testing your code... was much better than my idea by the way, but it seems not to work with multiple footnotes in the same paragraph.

Would be nice if the footnotes could be named for multiple references to the same footnote, e.g.:
my importent statement[[The important book((book))]] ...
and another statement[[((book))]] ...
________________
1. The important book

cmb
Posts: 14225
Joined: Tue Jun 21, 2011 11:04 am
Location: Bingen, RLP, DE
Contact:

Re: Footnotes

Post by cmb » Tue Mar 03, 2015 8:55 pm

svasti wrote:It seems I got the same request... :?
However testing your code... was much better than my idea by the way, but it seems not to work with multiple footnotes in the same paragraph.
Maybe pure coincidence. Maybe not.
svasti wrote:However testing your code... was much better than my idea by the way, but it seems not to work with multiple footnotes in the same paragraph.
I haven't tested it, but it is supposed to work. The code has a big shortcomming, though: it uses preg_match_all() and str_replace() to do the work, what might give wrong results. Most likely it would be better to use a single preg_replace_callback().
svasti wrote:Would be nice if the footnotes could be named for multiple references to the same footnote, e.g.:
my importent statement[[The important book((book))]] ...
and another statement[[((book))]] ...
Would make the code even more complex, though.
Christoph M. Becker – Plugins for CMSimple_XH

svasti
Posts: 1651
Joined: Wed Dec 17, 2008 5:08 pm

Re: Footnotes

Post by svasti » Tue Mar 03, 2015 9:10 pm

cmb wrote: Would make the code even more complex, though.
Both RefNotes Plugin for DokuWiki and Cite for MediaWiki have it. I have a site where lot's of footnotes are used... it's not in CMSimple though :( ,,, and indeed, sometimes I refer to the same reference.
svasti wrote:seems not to work with multiple footnotes in the same paragraph.
I checked several times, didn't work.

cmb
Posts: 14225
Joined: Tue Jun 21, 2011 11:04 am
Location: Bingen, RLP, DE
Contact:

Re: Footnotes

Post by cmb » Tue Mar 03, 2015 10:16 pm

svasti wrote:
svasti wrote:seems not to work with multiple footnotes in the same paragraph.
I checked several times, didn't work.
Yes, you're right. I didn't set the U flag for ungreedy matching, so that everything up to the last ]] on the same line was matched. I've already fixed it above.
svasti wrote:
cmb wrote: Would make the code even more complex, though.
Both RefNotes Plugin for DokuWiki and Cite for MediaWiki have it. I have a site where lot's of footnotes are used... it's not in CMSimple though :( ,,, and indeed, sometimes I refer to the same reference.
I didn't mean to say that it wouldn't make sense. It's just that it would make the code more complex, and I don't have the time to implement it. Feel free to use and improve the code under whatever license you prefer -- I don't claim any copyright.
Christoph M. Becker – Plugins for CMSimple_XH

wsim123
Posts: 63
Joined: Thu Feb 19, 2015 4:44 pm

Re: Footnotes

Post by wsim123 » Wed Mar 04, 2015 12:48 pm

cmb wrote:Hello Community,

I just received an email with the idea to have a simple syntax to add footnotes similar to MediaWiki and DokuWiki. The idea is to just write [[footnote text]] somewhere in the page content, and let CMSimple_XH turn that into a proper footnote. The following addition to cmsimple/userfuncs.php is supposed to do the trick:

Code: Select all

function convertFootnotes()
{
    global $s, $c, $su;

    $i = $s > -1 ? $s : 0;
    $count = preg_match_all(
        '/\[\[(.*)\]\]/U', $c[$i], $matches, PREG_SET_ORDER
    );
    if ($count) {
        $c[$i] .= tag('hr');
        foreach ($matches as $j => $match) {
            $c[$i] = str_replace(
                $match[0],
                '<sup><a href="#footnote' . ($j + 1)
                . '">[' . ($j + 1) . ']</a></sup>',
                $c[$i]
            );
            $c[$i] .= '<div id="footnote' . ($j + 1) . '">'
                . ($j + 1) . '. ' . $match[1] . '</div>';
        }
    }
}

if (!(XH_ADM && $edit)) {
    convertFootnotes();
}
Enjoy!

PS: slightly edited for ungreedy matching, see below for an explanation.
A further improvement would be to put the footnotes into a div or a p with by class stylable
border instead of a only common stylable hr on top.

Another improvement could be the possibility to jump back to the anchor from the footnote.

cmb
Posts: 14225
Joined: Tue Jun 21, 2011 11:04 am
Location: Bingen, RLP, DE
Contact:

Re: Footnotes

Post by cmb » Wed Mar 04, 2015 12:59 pm

wsim123 wrote:A further improvement would be to put the footnotes into a div or a p with by class stylable
border instead of a only common stylable hr on top.
Good idea. I've fixed that in the code above.
Christoph M. Becker – Plugins for CMSimple_XH

wsim123
Posts: 63
Joined: Thu Feb 19, 2015 4:44 pm

Re: Footnotes

Post by wsim123 » Fri Mar 06, 2015 11:58 am

wsim123 wrote:
cmb wrote:Hello Community,

I just received an email with the idea to have a simple syntax to add footnotes similar to MediaWiki and DokuWiki. The idea is to just write [[footnote text]] somewhere in the page content, and let CMSimple_XH turn that into a proper footnote. The following addition to cmsimple/userfuncs.php is supposed to do the trick:

Code: Select all

function convertFootnotes()
{
    global $s, $c, $su;

    $i = $s > -1 ? $s : 0;
    $count = preg_match_all(
        '/\[\[(.*)\]\]/U', $c[$i], $matches, PREG_SET_ORDER
    );
    if ($count) {
        $c[$i] .= tag('hr');
        foreach ($matches as $j => $match) {
            $c[$i] = str_replace(
                $match[0],
                '<sup><a href="#footnote' . ($j + 1)
                . '">[' . ($j + 1) . ']</a></sup>',
                $c[$i]
            );
            $c[$i] .= '<div id="footnote' . ($j + 1) . '">'
                . ($j + 1) . '. ' . $match[1] . '</div>';
        }
    }
}

if (!(XH_ADM && $edit)) {
    convertFootnotes();
}
Enjoy!

PS: slightly edited for ungreedy matching, see below for an explanation.
A further improvement would be to put the footnotes into a div or a p with by class stylable
border instead of a only common stylable hr on top.

Another improvement could be the possibility to jump back to the anchor from the footnote.
....................................................................................................................
Dieser Code funktioniert bei mir nicht. Beim drüberfahren des Links wird auch nur die Basis-Url angezeigt
mit #footnote2 und nicht die ganze URL.(Ich benutze aber die Maxim-Version).
Die obige erste Version funktioniert aber.

Man könnte es bei der Korrektur gleich so machen , dass man das hr auf height=0 setzt und das div
mit class=foot versieht. Dann hat man auch border-top als Trenner.
Ausserdem ist es bei Mediawiki möglich, ein <referenzes /> an eine beliebige stelle zu setzen und
dahinter weiteren Text. Das würde das ganze auf die gleiche Ebene komplettieren.
Last edited by wsim123 on Wed Mar 11, 2015 1:06 pm, edited 1 time in total.

svasti
Posts: 1651
Joined: Wed Dec 17, 2008 5:08 pm

Re: Footnotes

Post by svasti » Fri Mar 06, 2015 12:51 pm

cmb wrote:I don't have the time to implement it. Feel free to use and improve the code
Yesterday night I tried and here comes the enlarged code which enables multiple references to the same footnote.
Normal footnote:
  • text text text [[footnote]] text text
If you want to refer to the same footnote again, just give a little name to the footnote:
  • text text text [[footnote||name]] text text
and use this name where you want to point to the same footnote:
  • text text text [[name]] text text

Code: Select all

function convertFootnotes()
{
    global $s, $c, $su;

    $name = array();
    $i = $s > -1 ? $s : 0;
    $count = preg_match_all(
        '/\[\[(.*)\]\]/U', $c[$i], $matches, PREG_SET_ORDER
    );
    if ($count) {
        $c[$i] .= '<div class="footnotes">';
        $j = 1;
        foreach ($matches as $match) {
            $text = explode('||',$match[1]);
            if(in_array($text[0],$name)) {
                $nr = array_search($text[0],$name);
                $c[$i] = str_replace(
                    $match[0],
                    '<sup><a href="#footnote' . $nr
                    . '">[' . $nr . ']</a></sup>',
                    $c[$i]
                );
            } else {
                $c[$i] = str_replace(
                    $match[0],
                    '<sup><a href="#footnote' . $j
                    . '">[' . $j . ']</a></sup>',
                    $c[$i]
                );
                $c[$i] .= '<div id="footnote' . $j . '">'
                    . $j . '. ' . $text[0] . '</div>';
                if(isset($text[1])) $name[$j] = $text[1];
                $j++;
            }
        }
        $c[$i] .= '</div>';
    }
}

if (!(XH_ADM && $edit)) {
    convertFootnotes();
}
 

svasti
Posts: 1651
Joined: Wed Dec 17, 2008 5:08 pm

Re: Footnotes

Post by svasti » Sat Mar 07, 2015 3:07 pm

Habe inzwischen den Code noch weiter optimiert, z.B. indem die Fußnotenausgabe eine <ol> wird, was bei längeren Fußnoten schöner ist.

Leider funktioniert es nicht so gut mit CKEditor, denn der denkt, dass [[ ]] einen geschützten Bereich umschließt, und automatisch <span class="cke_placeholder" data-cke-placeholder="1">[[ ]]</span> daraus macht, wodurch man dorthin nicht mehr mit dem Cursor kommt. :cry:

Post Reply