Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

RE:(2) Automatic CODE tags

by swiftone (Curate)
on Jun 20, 2000 at 19:55 UTC ( #19043=note: print w/ replies, xml ) Need Help??


in reply to RE: Automatic CODE tags
in thread Automatic CODE tags

I think that a very simple code-detection could already be sufficient. If the post contains something, which remotely resembles Perl code, and it dosenīt contain a single CODE tag, then wrap the whole post in CODE tags.

A simple test would be something like:

if(Code-catcher-not-disabled){ foreach(@line-of_post){ if m?\</CODE\>?i{ { last; } eval { $_ }; #Actually something to do with Safe module if not $@ { $codify=1; } } }
(Requires clean up, and has many efficency improvements that can be made, but you get the idea. Basically if there is a line (or we could set a tolerance level...say, 4 lines?) of code that will eval, it's likely perl code. THis would also catch someone who remembered the CODE tag, but not the closing tag.
Note: This should be done in a sandbox, such as the Safe module, but I haven't toyed with that enough to include it in this pseudocode.

Another option is to use one of the Perl Parsers out there to check syntax, but that involves more overhead. eval already does all the checking, and properly buried in a Safe, it shouldn't affect the exterior code at all.


Comment on RE:(2) Automatic CODE tags
Download Code
RE: RE:(2) Automatic CODE tags
by Corion (Pope) on Jun 20, 2000 at 20:12 UTC

    Of course, there should be some more logic added, as most code that gets posted, won't compile. That's why it was posted in the first place. But then again, the analyzer should simply lean more towards code than towards text, because a monospaced text is far easier to read than vario-spaced and line-joined code.

      Of course, there should be some more logic added, as most code that gets posted, won't compile. That's why it was posted in the first place. But then again, the analyzer should simply lean more towards code than towards text, because a monospaced text is far easier to read than vario-spaced and line-joined code.

      I thought of that, but if you are taking it line-by-line (rather than whole post), any program of more than a few lines will have plenty of lines that compile.

      $var="bob";
      eval's, even if it doesn't do much. That's why I thought the code was so nicely elegant. You don't need much logic.

        I played around with the idea a bit yesterday night (the nights are really hot this time in .de :)), and here's a prototype that does some good formatting on my coding style. I don't know how well it works for other coding styles, and the implementation is a bit crude ... I couldn't get your elegant idea of "most single lines should compile" to work for me, so I resorted mostly to static REs - quite unelegant, as any such list is bound to be incomplete ...

        Anyway, if anybody wants to have a look at how a heuristic markup could be done, here's the code (the code is still a bit confused about CODE and PRE tags, but you should get the intention ...) :

        Update :D'oh - as soon as I go offline, I see many problems with the code. So here's an updated version ... Hmm - and I follow the suggestion and put the code into the Code Catacombs instead of posting it here ... The Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://19043]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (10)
As of 2014-09-15 04:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (145 votes), past polls