htmlperl

Convert all HTML list items to clickable hyperlinks


I've been given an HTML list to reformat. 120 questions and answers (240 tag sets total). All I need to do is make the text between the tags a link, like so:

<li>do snails make your feet itch?</li>

has to become

<li><a href="#n">do snails make your feet itch?</a></li>

I am performing this replacement in my IDE.

Eventually I'll likely try to do a batch replace with Perl to insert the 'n' variable so the links point properly.

You might ask: "if you can use Perl for that, why not the whole shebang?" ...that's a valid question, but I want to use regex more for the power it has for big lists like this. Plus my Perl skills are sketchy at best (I'd welcome Perl advice if it's on offer).


Solution

  • You can do it in two steps.

    1. Substitute <li> with <li><a href="#n">
    2. Substitute </li> with </a></li>

    Or you can try to be clever and it it in one. Here is a substitute command in Perl syntax ($1 references what was matched in the brackets).

    s,<li>(.*)</li>,<li><a href="#n">$1</a></li>,
    

    And while you are there it's easy to replace the second part of the replacement pattern with an expression that will increment n

    s,<li>(.*)</li>,q{<li><a href="#} .++$n . qq{">$1</a></li>},e
    

    See how you can run this from the command line:

    echo '<li>do snails make your feet itch?</li>' | 
    perl -pe 's,<li>(.*)</li>,q{<li><a href="#} .++$n . qq{">$1</a></li>},e'
    
    <li><a href="#1">do snails make your feet itch?</a></li>