in reply to perlmonks downloadable code blocks

I heard PM uses a simple regexp to identify code blocks and there's evidence* to support that. I don't know what the actual code is for displaytype=selectcode , displaytype=displaycode and displaytype=displaycode;part=X, but that same simple regexp would extract the code blocks quite easily. For example, the third page I mentioned could be generated with something as simple as
my $post_body = ...from db...; my $part = $cgi->param{part}; my $block = ($post_body =~ m{<c>(.*?)</c>}gi)[$part]; if (!defined($block)) { ...error... } else { print("Content-Type: text/plain\n\n$block"); }

* — PM recognizes <c>...</c> but not <c >...</c> like it does for other tags, and PM recognizes code blocks inside of HTML comments.

Replies are listed 'Best First'.
Re^2: perlmonks downloadable code blocks
by debiandude (Scribe) on Apr 11, 2008 at 07:40 UTC
    That's pretty snazzy. And I guess I could modify the code inline as well with something like this:
    my $match = 0; my $post_body = ...from db...; $match++ while $post_body =~ s/<c>(.*?)<\/c>/<a href='download?part=$m +atch'>Download Me<\/a><pre>$1<\/pre>/i;
      PM does a bit more than that — it escapes "<", ">" and "&", at least — but that's the idea.

      By the way, I'd add the "g" modifier to your s///. There's no reason to search from the start of the string every pass.

        Well if I use the global "/g" modifier it will replace all of my tags at once and $i will never be incremented.
Re^2: perlmonks downloadable code blocks
by debiandude (Scribe) on Apr 12, 2008 at 00:33 UTC
    The reason I wanted the while is because I update each code block with the number corresponding to which code block it is. Here is an example
    my $post_body ='<c>this is the first code block code</c> This is <c>an +other code block</c>'; my $part = 1; $part++ while $post_body =~ s/<c>(.*?)<\/c>/<a href='download?part=$p +art'>Download Me<\/a><pre>$1<\/pre>/i; print $post_body, "\n";
    And I will have this as an output:
    <a href='download?part=1'>Download Me</a><pre>this is code</pre> This +is <a href='download?part=2'>Download Me</a><pre>more code</pre>
    Am I missing something, because I still don't see how I can have the output that I want if I use the global switch.
      Ah good point. e modifier to the rescue.
      my $part; $post_body =~ s{<c>(.*?)<\/c>}{ ++part; qq{<a href='download?part=$part'>Download Me</a><pre>$1</pre>} }gei;