char *token = NULL; /* pointer to token */
int tokenlen;
-char *s = NULL; /* to return WHOLE hyphenated-word */
+static char *s = NULL; /* to return WHOLE hyphenated-word */
YY_BUFFER_STATE buf = NULL; /* buffer to parse; it need for parse from string */
+typedef struct {
+ int tlen;
+ int clen;
+ char *str;
+} TagStorage;
+
+static TagStorage ts={0,0,NULL};
+
+static void
+addTag() {
+ while( ts.clen+tsearch2_yyleng+1 > ts.tlen ) {
+ ts.tlen*=2;
+ ts.str=realloc(ts.str,ts.tlen);
+ if (!ts.str)
+ ereport(ERROR,
+ (errcode(ERRCODE_OUT_OF_MEMORY),
+ errmsg("out of memory")));
+ }
+ memcpy(ts.str+ts.clen,tsearch2_yytext,tsearch2_yyleng);
+ ts.clen+=tsearch2_yyleng;
+ ts.str[ts.clen]='\0';
+}
+
+static void
+startTag() {
+ if ( ts.str==NULL ) {
+ ts.tlen=tsearch2_yyleng+1;
+ ts.str=malloc(ts.tlen);
+ if (!ts.str)
+ ereport(ERROR,
+ (errcode(ERRCODE_OUT_OF_MEMORY),
+ errmsg("out of memory")));
+ }
+ ts.clen=0;
+ ts.str[0]='\0';
+ addTag();
+}
+
%}
%option 8bit
%%
-"<"[Ss][Cc][Rr][Ii][Pp][Tt] { BEGIN INSCRIPT; }
+"<"[Ss][Cc][Rr][Ii][Pp][Tt] { BEGIN INSCRIPT; startTag(); }
<INSCRIPT>"</"[Ss][Cc][Rr][Ii][Pp][Tt]">" {
BEGIN INITIAL;
- *tsearch2_yytext=' '; *(tsearch2_yytext+1) = '\0';
- token = tsearch2_yytext;
- tokenlen = tsearch2_yyleng;
- return SPACE;
+ addTag();
+ token = ts.str;
+ tokenlen = ts.clen;
+ return TAG;
}
-"<!--" { BEGIN INCOMMENT; }
+"<!--" { BEGIN INCOMMENT; startTag(); }
<INCOMMENT>"-->" {
BEGIN INITIAL;
- *tsearch2_yytext=' '; *(tsearch2_yytext+1) = '\0';
- token = tsearch2_yytext;
- tokenlen = tsearch2_yyleng;
- return SPACE;
+ addTag();
+ token = ts.str;
+ tokenlen = ts.clen;
+ return TAG;
}
-"<"[\![:alpha:]] { BEGIN INTAG; }
+"<"[\![:alpha:]] { BEGIN INTAG; startTag(); }
-"</"[[:alpha:]] { BEGIN INTAG; }
+"</"[[:alpha:]] { BEGIN INTAG; startTag(); }
-<INTAG>"\"" { BEGIN QINTAG; }
+<INTAG>"\"" { BEGIN QINTAG; addTag(); }
-<QINTAG>"\\\"" ;
+<QINTAG>"\\\"" { addTag(); }
-<QINTAG>"\"" { BEGIN INTAG; }
+<QINTAG>"\"" { BEGIN INTAG; addTag(); }
<INTAG>">" {
BEGIN INITIAL;
- token = tsearch2_yytext;
- *tsearch2_yytext=' ';
- token = tsearch2_yytext;
- tokenlen = 1;
+ addTag();
+ token = ts.str;
+ tokenlen = ts.clen;
return TAG;
}
-<QINTAG,INTAG,INCOMMENT,INSCRIPT>.|\n ;
+<QINTAG,INTAG,INCOMMENT,INSCRIPT>.|\n { addTag(); }
\&(quot|amp|nbsp|lt|gt)\; {
token = tsearch2_yytext;
tsearch2_yy_switch_to_buffer( buf );
BEGIN INITIAL;
}
+
#define IDIGNORE(x) ( (x)==13 || (x)==14 || (x)==12 || (x)==23 )
#define HLIDIGNORE(x) ( (x)==5 || (x)==13 || (x)==15 || (x)==16 || (x)==17 )
+#define HTMLHLIDIGNORE(x) ( (x)==5 || (x)==15 || (x)==16 || (x)==17 )
#define NONWORDTOKEN(x) ( (x)==12 || HLIDIGNORE(x) )
#define NOENDTOKEN(x) ( NONWORDTOKEN(x) || (x)==7 || (x)==8 || (x)==20 || (x)==21 || (x)==22 || IDIGNORE(x) )
curlen;
int i;
+ int highlight=0;
/* config */
prs->startsel = NULL;
prs->startsel = pstrdup(mptr->value);
else if (pg_strcasecmp(mptr->key, "StopSel") == 0)
prs->stopsel = pstrdup(mptr->value);
+ else if (pg_strcasecmp(mptr->key, "HighlightAll") == 0)
+ highlight = (
+ pg_strcasecmp(mptr->value, "1")==0 ||
+ pg_strcasecmp(mptr->value, "on")==0 ||
+ pg_strcasecmp(mptr->value, "true")==0 ||
+ pg_strcasecmp(mptr->value, "t")==0 ||
+ pg_strcasecmp(mptr->value, "y")==0 ||
+ pg_strcasecmp(mptr->value, "yes")==0 ) ?
+ 1 : 0;
pfree(mptr->key);
pfree(mptr->value);
}
pfree(map);
- if (min_words >= max_words)
- ereport(ERROR,
+ if (highlight==0) {
+ if (min_words >= max_words)
+ ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("MinWords should be less than MaxWords")));
- if (min_words <= 0)
- ereport(ERROR,
+ if (min_words <= 0)
+ ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("MinWords should be positive")));
- if (shortword < 0)
- ereport(ERROR,
+ if (shortword < 0)
+ ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("ShortWord should be >= 0")));
- }
-
- while (hlCover(prs, query, &p, &q))
- {
- /* find cover len in words */
- curlen = 0;
- poslen = 0;
- for (i = p; i <= q && curlen < max_words; i++)
- {
- if (!NONWORDTOKEN(prs->words[i].type))
- curlen++;
- if (prs->words[i].item && !prs->words[i].repeated)
- poslen++;
- pose = i;
}
+ }
- if (poslen < bestlen && !(NOENDTOKEN(prs->words[beste].type) || prs->words[beste].len <= shortword))
+ if (highlight==0) {
+ while (hlCover(prs, query, &p, &q))
{
- /* best already finded, so try one more cover */
- p++;
- continue;
- }
-
- posb=p;
- if (curlen < max_words)
- { /* find good end */
- for (i = i - 1; i < prs->curwords && curlen < max_words; i++)
+ /* find cover len in words */
+ curlen = 0;
+ poslen = 0;
+ for (i = p; i <= q && curlen < max_words; i++)
{
- if (i != q)
+ if (!NONWORDTOKEN(prs->words[i].type))
+ curlen++;
+ if (prs->words[i].item && !prs->words[i].repeated)
+ poslen++;
+ pose = i;
+ }
+
+ if (poslen < bestlen && !(NOENDTOKEN(prs->words[beste].type) || prs->words[beste].len <= shortword))
+ {
+ /* best already finded, so try one more cover */
+ p++;
+ continue;
+ }
+
+ posb=p;
+ if (curlen < max_words)
+ { /* find good end */
+ for (i = i - 1; i < prs->curwords && curlen < max_words; i++)
{
- if (!NONWORDTOKEN(prs->words[i].type))
- curlen++;
- if (prs->words[i].item && !prs->words[i].repeated)
- poslen++;
+ if (i != q)
+ {
+ if (!NONWORDTOKEN(prs->words[i].type))
+ curlen++;
+ if (prs->words[i].item && !prs->words[i].repeated)
+ poslen++;
+ }
+ pose = i;
+ if (NOENDTOKEN(prs->words[i].type) || prs->words[i].len <= shortword)
+ continue;
+ if (curlen >= min_words)
+ break;
+ }
+ if ( curlen < min_words && i>=prs->curwords ) { /* got end of text and our cover is shoter than min_words */
+ for(i=p; i>= 0; i--) {
+ if (!NONWORDTOKEN(prs->words[i].type))
+ curlen++;
+ if (prs->words[i].item && !prs->words[i].repeated)
+ poslen++;
+ if (NOENDTOKEN(prs->words[i].type) || prs->words[i].len <= shortword)
+ continue;
+ if (curlen >= min_words)
+ break;
+ }
+ posb=(i>=0) ? i : 0;
}
- pose = i;
- if (NOENDTOKEN(prs->words[i].type) || prs->words[i].len <= shortword)
- continue;
- if (curlen >= min_words)
- break;
}
- if ( curlen < min_words && i>=prs->curwords ) { /* got end of text and our cover is shoter than min_words */
- for(i=p; i>= 0; i--) {
+ else
+ { /* shorter cover :((( */
+ for (; curlen > min_words; i--)
+ {
if (!NONWORDTOKEN(prs->words[i].type))
- curlen++;
+ curlen--;
if (prs->words[i].item && !prs->words[i].repeated)
- poslen++;
+ poslen--;
+ pose = i;
if (NOENDTOKEN(prs->words[i].type) || prs->words[i].len <= shortword)
continue;
- if (curlen >= min_words)
- break;
+ break;
}
- posb=(i>=0) ? i : 0;
}
- }
- else
- { /* shorter cover :((( */
- for (; curlen > min_words; i--)
+
+ if (bestlen < 0 || (poslen > bestlen && !(NOENDTOKEN(prs->words[pose].type) || prs->words[pose].len <= shortword)) ||
+ (bestlen >= 0 && !(NOENDTOKEN(prs->words[pose].type) || prs->words[pose].len <= shortword) &&
+ (NOENDTOKEN(prs->words[beste].type) || prs->words[beste].len <= shortword)))
{
- if (!NONWORDTOKEN(prs->words[i].type))
- curlen--;
- if (prs->words[i].item && !prs->words[i].repeated)
- poslen--;
- pose = i;
- if (NOENDTOKEN(prs->words[i].type) || prs->words[i].len <= shortword)
- continue;
- break;
+ bestb = posb;
+ beste = pose;
+ bestlen = poslen;
}
+
+ p++;
}
- if (bestlen < 0 || (poslen > bestlen && !(NOENDTOKEN(prs->words[pose].type) || prs->words[pose].len <= shortword)) ||
- (bestlen >= 0 && !(NOENDTOKEN(prs->words[pose].type) || prs->words[pose].len <= shortword) &&
- (NOENDTOKEN(prs->words[beste].type) || prs->words[beste].len <= shortword)))
+ if (bestlen < 0)
{
- bestb = posb;
+ curlen = 0;
+ for (i = 0; i < prs->curwords && curlen < min_words; i++)
+ {
+ if (!NONWORDTOKEN(prs->words[i].type))
+ curlen++;
+ pose = i;
+ }
+ bestb = 0;
beste = pose;
- bestlen = poslen;
}
-
- p++;
- }
-
- if (bestlen < 0)
- {
- curlen = 0;
- poslen = 0;
- for (i = 0; i < prs->curwords && curlen < min_words; i++)
- {
- if (!NONWORDTOKEN(prs->words[i].type))
- curlen++;
- pose = i;
- }
- bestb = 0;
- beste = pose;
+ } else {
+ bestb=0;
+ beste=prs->curwords-1;
}
for (i = bestb; i <= beste; i++)
{
if (prs->words[i].item)
prs->words[i].selected = 1;
- if (prs->words[i].repeated)
- prs->words[i].skip = 1;
- if (HLIDIGNORE(prs->words[i].type))
- prs->words[i].replace = 1;
+ if ( highlight==0 ) {
+ if (HLIDIGNORE(prs->words[i].type))
+ prs->words[i].replace = 1;
+ } else {
+ if (HTMLHLIDIGNORE(prs->words[i].type))
+ prs->words[i].replace = 1;
+ }
- prs->words[i].in = 1;
+ prs->words[i].in = (prs->words[i].repeated) ? 0 : 1;
}
if (!prs->startsel)