[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
lynx-dev lynx html source colorization program
From: |
Vlad Harchev |
Subject: |
lynx-dev lynx html source colorization program |
Date: |
Mon, 15 Mar 1999 07:28:46 +0400 (SAMT) |
Here is a progam that can be used to colorize the source in lynx. I've
written it today. Just 'flex file.l && cc lex.yy.c'. It's a filter.
It can be easily integrated in lynx - since flex is very flexible. Try it
out. I've used flex-2.5.4. From a given html file it produces another html
file, with tags encapsulated and source formatting preserved. Ie, if the
source file contains:
<a href="foo.html"> a reference </a>
it will produce (exact output is presented)
<HTML><HEAD><TITLE>source</TITLE></HEAD><BODY><PRE>
<b><</b><em>A HREF=<tt><a href="foo.html">"foo.html"</a>
</tt></em><b>></b> a reference <b></</b><em>A</em><b>></b>
</PRE></BODY></HTML>
Best regards,
-Vlad
------------------
/* by Vlad Harchev <address@hidden> */
%x INTAG INTAG_END GETTAG COMMENT TAG_PARAM
%option noyywrap
%{
#include <ctype.h>
#include <stdio.h>
typedef char* str;
typedef char c_t;
c_t tmp_c;
char is_href; int dump_href;
/* this will convert the string to upper case inplace */
void pvt_strtoupper(char* str,int len)
{
char* end=str+len; --str;
while (++str < end ) *str = toupper(*str);
};
#define abr_s "<b>"
#define abr_e "</b>"
#define preamble_s "<h1>"
#define preamble_e "</h1>"
#define val_s "<tt>"
#define val_e "</tt>"
#define param_s "<em>"
#define param_e "</em>"
#define com_s "<tt>"
#define com_e "</tt>"
#define lt "<"
#define gt ">"
#define q """
#define amp "&"
#define t_up() pvt_strtoupper(yytext,yyleng);
#define _tag(n) cur_attrs=n##_a; t_up(); puts_n(yytext,yyleng);
/* if x is string literal, then we can compute it's len at compile time*/
#define puts_(x) fwrite(x,(sizeof x)-1,1,stdout)
/* if x is ptr to null terminated str, then we compute it's len at run time*/
#define puts__(x) fputs(x,stdout)
/* if x is ptr, and we must write n chars*/
#define puts_n(x,n) fwrite(x,n,1,stdout)
str c_a[]={"HREF",0}; /*common names */
typedef str* pstr;
str body_a[]={"BACKGROUND",0}, form_a[]={"ACTION",0},
img_a[]={"SRC","USEMAP",0}, input_a[]={"SRC",0}, unknown_a[]={ 0 };
pstr a_a=c_a, link_a=c_a,area_a=c_a,base_a=c_a;
pstr cur_attrs;
void make_href(char* str,int len)
{
puts_("<a href=\"") ; puts_n(str,len); puts_("\">");
}
%}
WHITE [ \t\n]
%%
"<!--" puts_(com_s lt "!--" ); BEGIN(COMMENT);
<COMMENT>{
"-->" puts_( "--" gt com_e ); BEGIN(0);
"<" puts_(lt);
">" puts_(gt);
"&" puts_(amp);
\" puts_(q);
}
<INTAG_END>{
> puts_(param_e abr_s gt abr_e); BEGIN(0);
{WHITE}+ puts_(" ");/*pack whites*/
[^ \t\n] t_up(); puts_n(yytext,yyleng);
}
<GETTAG>{
[aA]/({WHITE}*|">") _tag(a); BEGIN(INTAG);
[bB][oO][dD][yY]/({WHITE}*|">") _tag(body);BEGIN(INTAG);
[fF][oO][rR][mM]/({WHITE}*|">") _tag(form);BEGIN(INTAG);
[iI][mM][gG]/({WHITE}*|">") _tag(img);BEGIN(INTAG);
[iI][nN][pP][uU][tT]/({WHITE}*|">") _tag(input);BEGIN(INTAG);
[lL][iI][nN][kK]/({WHITE}*|">") _tag(link);BEGIN(INTAG);
[aA][rR][eE][aA]/({WHITE}*|">") _tag(area);BEGIN(INTAG);
[bB][aA][sS][eE]/({WHITE}*|">") _tag(base);BEGIN(INTAG);
([^ \t\n>]+)/({WHITE}*|">") _tag(unknown); BEGIN(INTAG);
}
"<"{WHITE}* puts_(abr_s lt abr_e param_s ); BEGIN(GETTAG);
"</"{WHITE}* puts_(abr_s lt "/" abr_e param_s ); BEGIN(INTAG_END);
<INTAG>{
{WHITE}*">" puts_(param_e abr_s gt abr_e ) ; BEGIN(0);
{WHITE}+ puts_(" ");
[^ \t\n=>]+ {
int i=0;
t_up(); puts_n(yytext,yyleng); dump_href=0;
while (cur_attrs[i])
{ /* find out whether this tag has attribute that from
which hyperlink can be created */
if ( !strcmp(yytext,cur_attrs[i]) )
{ dump_href=1;break; };
++i;
} ;
BEGIN(TAG_PARAM);
}
}
/*we are waiting for tag parameter */
<TAG_PARAM>{
">" puts_(param_e abr_s gt abr_e ) ; BEGIN(0);
{WHITE}+ puts_(" ");BEGIN(INTAG);/*no parameter*/
=[^\"'][^ \t\n>]+ {
puts_("=" val_s);
if (dump_href) make_href(yytext+1,yyleng-1);
puts_n(yytext+1,yyleng-1);
if (dump_href) puts_( "</a>");
puts_( val_e ); BEGIN(INTAG);
}
=\"[^\"]*\" {
puts_("=" val_s );
if (dump_href) make_href(yytext+2,yyleng -2 -1);
puts_( q ); puts_n(yytext+2,yyleng -2 -1); puts_( q );
if (dump_href) puts_( "</a>");
puts_( val_e ); BEGIN(INTAG);
}
='[^']*' {
puts_("=" val_s );
if (dump_href) make_href(yytext+2,yyleng -2 -1);
puts_("'"); puts_n(yytext+2,yyleng -2 -1);puts_("'");
if (dump_href) puts_( "</a>");
puts_( val_e); BEGIN(INTAG);
};
}
%%
void main()
{
printf("<HTML><HEAD><TITLE>source</TITLE></HEAD><BODY><PRE>");
yylex();
printf("</PRE></BODY></HTML>");
};
- lynx-dev lynx html source colorization program,
Vlad Harchev <=