LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Using mapping in VIM to add HTML? (https://www.linuxquestions.org/questions/linux-newbie-8/using-mapping-in-vim-to-add-html-723538/)

gaikokujinkyofusho 05-04-2009 07:42 AM

Using mapping in VIM to add HTML?
 
Hi, I am trying to figure out how to make VIM automatically (if it is possible, though most things are with vim) turn a text file into an HTML file.

That is, I have a list of movies that i have, the list contains the Directory Path and the name of the movie like this:
SciFi & Fantasy\Alien Raiders (2008)
SciFi & Fantasy\Cube (1997)
SciFi & Fantasy\Gattaca
SciFi & Fantasy\Equilibrium
SciFi & Fantasy\Day Watch
SciFi & Fantasy\Men In Black
SciFi & Fantasy\Sixth Sense
SciFi & Fantasy\Next (2007)
SciFi & Fantasy\Night Watch
SciFi & Fantasy\Men In Black 2

I want to make it look like this

SciFi & Fantasy\<a href="http://www.imdb.com/find?s=all&q=Alien+Raiders+(2008)&x=0&y=0">Alien Raiders (2008)</a><br>
SciFi & Fantasy\<a href="http://www.imdb.com/find?s=all&q=Cube+(1997)&x=0&y=0">Cube (1997)</a><br>
SciFi & Fantasy\<a href="http://www.imdb.com/find?s=all&q=Gattaca&x=0&y=0">Gattaca</a><br>
SciFi & Fantasy\<a href="http://www.imdb.com/find?s=all&q=Equilibrium&x=0&y=0">Equilibrium</a><br>
SciFi & Fantasy\<a href="http://www.imdb.com/find?s=all&q=Day+Watch&x=0&y=0">Day Watch</a><br>
SciFi & Fantasy\<a href="http://www.imdb.com/find?s=all&q=Men+In+Black&x=0&y=0">Men In Black</a><br>
SciFi & Fantasy\<a href="http://www.imdb.com/find?s=all&q=Sixth+Sense&x=0&y=0">Sixth Sense</a><br>
SciFi & Fantasy\<a href="http://www.imdb.com/find?s=all&q=Next+(2007)&x=0&y=0">Next (2007)</a><br>
SciFi & Fantasy\<a href="http://www.imdb.com/find?s=all&q=Night+Watch&x=0&y=0">Night Watch</a><br>
SciFi & Fantasy\<a href="http://www.imdb.com/find?s=all&q=Men+In+Black+2&x=0&y=0">Men In Black 2</a><br>

about as far as i have gotten is this:
:map _ $^[?\a<a href="http://www.imdb.com/find?s=all&q=

but i am not sure how to copy the text then add the "+" then paste etc

Any help would really be appreciated!!

Cheers

-Gaiko

dave.donaghy 05-04-2009 02:46 PM

Looks like you need to take everything from the backslash until the end of the line, and surround the remainder with a block of text that duplicates the text.

So XXX\YYY becomes XXX\<a ...YYY>YYY</a> (well, pretty much ...)

Part of it is tricky, cos you need to do a bit of jiggery pokery with the spaces (turning them into +s), but let's gloss over that for now ...

:%s/\\\(.*\)/\\<a href=...\1...>\1<\\a><br>/

The triple-\ is a bit of a red herring: the first two represent an escaped backslash (cos you've really got backslashes in your text file) and the next one opens up a \(-\) block, which surrounds a block of text that we'll pick up later with \1, which occurs twice in the replacement text, cos you really do want to see it twice in your HTML output.

To turn the spaces into +s, you need to get a bit fiddly: you could replace them all first, and then turn some of them back afterwards, I guess. Can't produce the fiddly vim business of the top of my head right now, but if I think of something inspired, I'll post it.

By the way, I'm not sure why you want to do this with mapping: I guess it would let you store the mapping and use it loads of times, but I'm not sure if you really need to do that, or if you just think that mapping is cool. :)

Using sed might be a bit easier: the commands are pretty much the same as vi, but it's non-interactive. (That's not the complete truth of the distinction between vi and sed, but it's close.)

Hope this helps.

Poetics 05-04-2009 06:43 PM

While I'm not sure about doing it natively in VIM, the above shouldn't take more than a few lines of perl, or even with judicious use of cat, awk, and a temp file. In either case, I'm interested in what you come up with.

gaikokujinkyofusho 05-04-2009 11:57 PM

Hello and thanks to Dave for his input. I had also posted on the comp.editors newsgroup and one of the regex masters suggested the following which worked flawlessly:

%s/\\\zs[^\\]*$/\=printf('<a href="http:\/\/www.imdb.com\/find?s=all&q=%s&x=0&y=0">%s<\/a><br>',join(split(submatch(0),' '),'+'), submatch(0))/

As for why i wanted mapping, i didn't i'm just a noob and didn't know i could run a command like that, for some reason i thought it had to be mapped.

Cheers (and thanks for the input!)

-Gaiko


All times are GMT -5. The time now is 10:47 PM.