in reply to Re: Template Parsing - Finding tag pairs.
in thread Template Parsing - Finding tag pairs.

I suppose that HTML::Parser is unusable for CF. The trick is that parser must notice only CF tags. Text document with CF tags can easily look as invalid html document for HTML::Parser in many cases.

--
Ilya Martynov (http://martynov.org/)

  • Comment on Re: Re: Template Parsing - Finding tag pairs.

Replies are listed 'Best First'.
Re: Re: Re: Template Parsing - Finding tag pairs.
by Juerd (Abbot) on Dec 26, 2001 at 00:14 UTC
    I'm not familiar with CFML. There might be some tags that would make invalid syntax, but if there are not, $htmlparser->report_tags(qw/cfif cfelseif cfelse cfoutput cfinclude cfetcetera/) might provide for easy parsing using event subs.

    2;0 juerd@ouranos:~$ perl -e'undef christmas' Segmentation fault 2;139 juerd@ouranos:~$

      Simple example. How it will handle CF tags inside HTML tags? Or CF tags in text non-HTML documents (which can contains anything)?

      --
      Ilya Martynov (http://martynov.org/)

        I guess sharing a stack would help...
        # this code is missing a lot. don't expect it to work :) { my @stack; sub start { push @stack, $tag; } sub end { if ($tag eq 'cfif' and $stack[-1] eq 'cfelse'){ pop @stack; } die "Invalid code" if pop(@stack) ne $tag; } sub text { # use @stack to determine where we are... } } my $parser = HTML::Parser->new(start_h => [\&start, 'tagname'], end_h => [\&end, 'tagname'], text => [\&text, 'text'], ); $parser->report_tags(qw/cfif cfelse cfend/); $parser->parse($cfml);
        ---
        <body><cfif>foo<cfelse><b>bar</b></cfif></body> ==> text '<body>'; start 'cfif'; text 'foo'; start 'cfelse'; text '<b>bar</b>'; end 'cfif';

        2;0 juerd@ouranos:~$ perl -e'undef christmas' Segmentation fault 2;139 juerd@ouranos:~$

        Precisely to the point IlyaM ;-). It all boils down to parsing a CFML code like this:
        <cfset var = "foobar"> <cfset bool = "true"> <cfif bool eq "true"> print something here if bool is true. <cfelse> <cfif var eq "foobar"> foobar... Will dump query: <cfquery name="foobar_query"> #foo#, #bar# </cfquery> <cfoutput>foobar = #foobar#</cfoutput> </cfif> </cfif>
        This has nothing to do with HTML and therefore, using HTML::Parser(s) is either an overkill or plain useless in my case. I've posted a node in meditations explaining my design behind the module (ColdFusion parser). Read more here.

        "There is no system but GNU, and Linux is one of its kernels." -- Confession of Faith