LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 05-04-2009, 07:42 AM   #1
gaikokujinkyofusho
LQ Newbie
 
Registered: Apr 2009
Location: different day, different place
Distribution: SuSE, considering Mint
Posts: 14

Rep: Reputation: 0
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
 
Old 05-04-2009, 02:46 PM   #2
dave.donaghy
LQ Newbie
 
Registered: Apr 2009
Posts: 6

Rep: Reputation: 1
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.
 
Old 05-04-2009, 06:43 PM   #3
Poetics
Senior Member
 
Registered: Jun 2003
Location: California
Distribution: Slackware
Posts: 1,178

Rep: Reputation: 49
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.
 
Old 05-04-2009, 11:57 PM   #4
gaikokujinkyofusho
LQ Newbie
 
Registered: Apr 2009
Location: different day, different place
Distribution: SuSE, considering Mint
Posts: 14

Original Poster
Rep: Reputation: 0
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Please add a group for Editors like Xemacs, VIM, etc. MrUmunhum LQ Suggestions & Feedback 1 08-26-2008 03:11 PM
LXer: Mind mapping tools add new dimensions to old technique LXer Syndicated Linux News 0 08-06-2007 07:31 PM
vim add # automatically noir911 Linux - Software 11 09-13-2006 10:56 PM
mapping a key to compile a program in vim dvm Linux - Software 1 01-26-2005 07:56 AM
Mapping Esc to windows key in Vim frankie_DJ Linux - Newbie 2 10-14-2004 02:56 PM


All times are GMT -5. The time now is 05:45 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration