i have a html-file with footnotes converted to plain text and
like to replace the footnotes in the text with the
footnotetext many pages later, eg. the occurrence of [12] with
the later definition (many pages later) like [12] "bla bla
bla." Does anybody has an idea, how to achieve this?

Without deeper detail about your file, I'll take a shot. Some assumptions include that a footnote is on one line, that a footnote can be found with "^\[\d\+] as a regexp

My basic thought would be to do something like the following logic:

1) use a ":g" command to perform an action on each of my known footnotes, as identified by some regexp

2) compose a ":s" command as a string that searches for references to the given footnote

3) :exec the resulting string.


My first pass would look something like this 100% untested shot:

:g/^\[\d\+]/exec
        '%s@'.
        substitute(
                escape(
                        getline('.'),
                        '@[\\.*&'
                        ),
                '\(\[\d\+]\).*',
                '\1@&',
                ''
                )

(broken into multiple lines to make it easier to parse, though it should all be on one line). I'm sure I forgot some of the characters that need to be escaped, and the resulting regexp might not be quite what I expected it to be, but it should be pretty close. You can proof it by changing the "exec" to "echo" so it doesn't actually change anything.

You might want to specify ranges, if your footnotes (or rather "endnotes") begin at line 3141 in your file, you might alter the above to

:3141,$g/^\[\d\+]/exec
        '1,3140s@'.
        ...

so that you're not changing the footnote itself (assuming they're end-notes rather than foot-notes)

Another method might be to assume that footnotes are sequential and do something like:

:let fn=1
:while search('^\['.fn.']', 'w') > 0 | let fn_text = getline('.') | exec '[EMAIL PROTECTED]'.fn.']@'.escape(fn_text, '@[\\.*&') | let fn+=1 | endwhile

Neither is particularly pretty, elegant, robust, or comprehensible (more like "reprehensible" :) but they're a first pass at ideas on how I'd tackle it.

There are problems when an in-line footnote happens to have found its way to the beginning of a line and thus get confused for being the actual footnote contents, but otherwise, the above should be enough to get you started.

-tim





Reply via email to