dwp-forge / refnotes

4 stars 6 forks source link

Help: trying to adapt syntax structure to make it compatible with Pandoc #61

Closed Yaco closed 7 months ago

Yaco commented 3 years ago

Hi! I am trying to make a small fork of this great plugin in order to be able to write footnotes following Pandoc's markdown definition (see here: https://pandoc.org/MANUAL.html#footnotes). This is an example:

That's some text with a footnote.[^1]

[^1]: And that's the footnote.

I started to edit references.php file, the initializePatterns function, in the following way:

    private function initializePatterns() {
        if (refnotes_configuration::getSetting('replace-footnotes')) {
            $entry = '(?:\(\(|\[\()';
            $exit = '(?:\)\)|\)\])';
            $id = '@@FNT\d+|#\d+';
        }
        else {
            $entry = '\[\^';
            $exit = '\]';
            $id = '#\d+';
        }

        $strictName = refnotes_note::getNamePattern('strict');
        $extendedName = refnotes_note::getNamePattern('extended');
        $namespace = refnotes_namespace::getNamePattern('optional');

        $text = '.*?';

        $strictName = '(?:' . $id . '|' . $strictName . ')';
        $fullName = '\s*(?:' . $namespace . $strictName . '|:' . $namespace . $extendedName . ')\s*';
        $lookaheadExit = '(?=' . $exit . ')';
        $nameEntry = $fullName . $lookaheadExit;

        $extendedName = '(?:' . $id . '|' . $extendedName . ')';
        $optionalFullName = $extendedName . '?';
        $structuredEntry = '\s*' . $optionalFullName . '\s*>>' . $text  . $lookaheadExit;

        $define = '\s*' . $optionalFullName . '\s*]:\s*';
        $optionalDefine = '(?:' . $define . ')?';
        $lookaheadExit = '(?=' . $text . $exit . ')';
        $defineEntry = $optionalDefine . $lookaheadExit;

        $this->entryPattern = $entry . '(?:' . $nameEntry . '|' . $structuredEntry . '|' . $defineEntry . ')';
        $this->exitPattern = $exit;
        $this->handlePattern = '/' . $entry . '\s*(' . $optionalFullName . ')\s*(?:>>(.*))?(.*)/s';
    }

Still don't getting the desired results, some I am kind of stuck. Maybe someone can help me to do the final changes?

Thanks!

dwp-forge commented 3 years ago

The idea of entry and exit patterns is to define the scope where your plugin takes over parsing from DW core engine. With the syntax above, the ": And that's the footnote." part will not be handled by your plugin.

Yaco commented 3 years ago

Thanks @dwp-forge, I know the code is not working as I need. Currently, my latest code is able to parse this format:

That's some text with a footnote.[^1]

[^1:] And that's the footnote. ]

Could you help me to make it work as expected? You have much more expertise with regex than me for sure. Thanks!

dwp-forge commented 3 years ago

If you really want to get something working the way you want it, then, well, you will have to raise your expertise level. Others can point you in the right direction, but don't expect them doing the stuff for you just because you ask nicely ;)

This plugin has rather complex syntax and just changing [( to [^ may not magically give you the outcome you desire. You have to first understand the existing syntax and how it is implemented, decide which parts you need and which you can drop, and finally figure out how to implement the parts you want to keep in your new syntax. All this will require time and effort, and you are the only one who is interested to invest them.