Sunday, November 26, 2017

Re: using vim to add <a href= ...> links to an epub index file

On Saturday, November 25, 2017 at 10:25:32 AM UTC-8, Chris Jones wrote:
> On Fri, Nov 24, 2017 at 12:43:46PM EST, porphyry5 wrote:
> > On Thursday, November 23, 2017 at 3:53:58 PM UTC-8, Chris Jones wrote:
>
> [..]
> >
> > Substitute (:h :s) will do all you need. In the case of links and
> > anchors, I modify this model to the specific situation in each case:
> >
> > :%s/ \(_\(\w\+\)\)/ <a href="#\1">\2<\/a>/g|:%s/^_\w\+$/<a > name="&"><\/a>/
>
> Do you mean using submatch(0) to retrieve what /\d\+< actually matched
> in the current iteration?
>
> So far this seems to be the only way to retrieve the string that a regex
> actually matches... alas, as per the :help submatch vim manual...
> submatch() can only be used in the context of the replacement part of
> a :substitute command - which is not what I had in mind.
>
> Just curious. I gave up on the idea of using vim in this instance and
> wrote a ~10 lines python script that rewrites the file... adding the
> links where relevant.
>
> > Being simple minded, I just ensure that anchors always occur at the
> > start of lines, and that links never do.
>
> Always try to eat off of a clean plate when you can. The index file as
> tidied up by yours truly was nice and clean to start with... My little
> script only created ~10 faulty <a href= > links out of the 6,000+...
> which took c. 10 minutes to edit.
>
> All the same & just for the hell of it... doing it in vim would have
> been more satisfying.
>
> So if you could afford the time... could you explain the vim solution
> you had in mind? I'm still interested.
>
> Thanks,
>
> CJ

I was referring to the :substitute command, which can use submatch() if need be, but usually is not necessary.
Entering :h :s<Enter> at the command line invokes the help for :substitute. :s is usually employed, being the shortest abbreviation of :substitute vim recognizes
There is an associated function substitute(), which works almost identically to :substitute

You really need to read the help chapters usr_27.txt and pattern.txt (:h usr_27 and :h pattern), I cannot possibly give a brief overview of vim's pattern matching and manipulating ability.

Largely I correct ocr-ed texts and convert them to .txt, .html and .epub. The 2 :s command string I supplied is literally all I ever need to produce the Page No. anchors and within Index links (occasionally I may need to use a minor modification of the pattern). But I do this early in the conversion process, when it is simple to differentiate links from anchors. You have left yours until much later, so your pattern will be more complex, but the general principles still apply.

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments: