files: core/bbcodeparser.h core/bbcodeparser.cpp git-svn-id: svn://ttmath.org/publicrep/winix/trunk@615 e52654a7-88a9-db11-a3e9-0013d4bc506epull/3/head
parent
759135fd7d
commit
16bb238518
@ -1 +1 @@
|
||||
o = acceptbaseparser.o compress.o config.o data.o db.o db_itemcolumns.o dircontainer.o dirs.o function.o functioncodeparser.o functionparser.o functions.o groups.o htmlfilter.o httpsimpleparser.o lastcontainer.o loadavg.o locale.o log.o misc.o mount.o mountparser.o mounts.o notify.o plugin.o plugindata.o postmultiparser.o rebus.o request.o requestcontroller.o session.o sessioncontainer.o sessionmanager.o sessionparser.o users.o
|
||||
o = acceptbaseparser.o bbcodeparser.o compress.o config.o data.o db.o db_itemcolumns.o dircontainer.o dirs.o function.o functioncodeparser.o functionparser.o functions.o groups.o htmlfilter.o httpsimpleparser.o lastcontainer.o loadavg.o locale.o log.o misc.o mount.o mountparser.o mounts.o notify.o plugin.o plugindata.o postmultiparser.o rebus.o request.o requestcontroller.o session.o sessioncontainer.o sessionmanager.o sessionparser.o users.o
|
||||
|
@ -0,0 +1,566 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is not publicly distributed
|
||||
*
|
||||
* Copyright (c) 2008-2010, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "bbcodeparser.h"
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
bool BBCODEParser::IsValidCharForName(int c)
|
||||
{
|
||||
if( (c>='a' && c<='z') ||
|
||||
(c>='A' && c<='Z') ||
|
||||
c=='*' || c=='_')
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
bool BBCODEParser::IsOpeningTagMark()
|
||||
{
|
||||
return (*pchar == '[');
|
||||
}
|
||||
|
||||
|
||||
// there are no commentaries in bbcode
|
||||
bool BBCODEParser::IsOpeningCommentaryTagMark()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
bool BBCODEParser::SkipCommentaryTagIfExists()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
bool BBCODEParser::IsClosingTagMark()
|
||||
{
|
||||
return (*pchar == ']');
|
||||
}
|
||||
|
||||
|
||||
bool BBCODEParser::IsClosingXmlSimpleTagMark()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// one enter will generate one <br>
|
||||
// two enters or more will generate only two br (<br><br>)
|
||||
void BBCODEParser::PutNormalText(const char * str, const char * end)
|
||||
{
|
||||
int br_len;
|
||||
|
||||
if( *pchar == 0 )
|
||||
{
|
||||
// trimming last white characters at end of the user text
|
||||
while( str<end && (IsWhite(*(end-1)) || *(end-1)==10) )
|
||||
--end;
|
||||
}
|
||||
|
||||
|
||||
while( str < end )
|
||||
{
|
||||
if( *str == 10 )
|
||||
{
|
||||
++str;
|
||||
br_len = 1;
|
||||
|
||||
// skipping white characters without a new line character
|
||||
while( str < end && IsWhite(*str) )
|
||||
++str;
|
||||
|
||||
if( str < end && *str == 10 )
|
||||
{
|
||||
br_len = 2;
|
||||
|
||||
// skipping white characters with new line characters
|
||||
while( str < end && (IsWhite(*str) || *str==10) )
|
||||
++str;
|
||||
}
|
||||
|
||||
if( !has_open_ol_tag && !has_open_ul_tag && !has_open_li_tag )
|
||||
{
|
||||
for(int i=0 ; i < br_len ; ++i)
|
||||
(*out_string) += "<br>\n";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
PrintEscape(*str);
|
||||
++str;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void BBCODEParser::PutNormalTextTrim(const char * str, const char * end)
|
||||
{
|
||||
// we don't use trimming in bbcode parser
|
||||
PutNormalText(str, end);
|
||||
}
|
||||
|
||||
|
||||
void BBCODEParser::ReadNormalTextSkipWhite(const char * & start, const char * & last_non_white)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void BBCODEParser::CheckExceptions()
|
||||
{
|
||||
if( stack_len >= 2 )
|
||||
{
|
||||
if( pstack[stack_len-1].type == Item::opening &&
|
||||
pstack[stack_len-2].type == Item::opening &&
|
||||
IsNameEqual("*", pstack[stack_len-1].name) &&
|
||||
IsNameEqual("*", pstack[stack_len-2].name) )
|
||||
{
|
||||
// removing the last [*] from the stack
|
||||
// </li> was put automatically
|
||||
PopStack();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
bbcode format:
|
||||
[bbcodetag=value]some text[/bbcodetag]
|
||||
the value can be quoted, e.g.
|
||||
[bbcodetag="value"]some text[/bbcodetag], or
|
||||
[bbcodetag='value']some text[/bbcodetag]
|
||||
|
||||
the third string below (in tags table) is 'html_argument' from Tags,
|
||||
it can contain a special character % followed by a string which means:
|
||||
%1 - "value" escaped as for html
|
||||
%2 - "some text" escaped as for html
|
||||
%u1 - "value" trimmed and escaped as for url-es
|
||||
%u2 - "some text" trimmed and escaped as for url-es
|
||||
%% - one %
|
||||
|
||||
if you are using %2 or %u2 then "some text" is not treated as bbcode, e.g.
|
||||
[bbcodetag=value]some [b]text[/b][/bbcodetag] will produce:
|
||||
<htmltag arg="value">some [b]text[/b]</htmltag> (the inner tags [b][/b] were not parsed)
|
||||
|
||||
also when using %2 or %u2 the closing bbcode tag is skipped
|
||||
(if you want this tag then you can put it in 'html_argument')
|
||||
|
||||
and when using u (%u1 or %u2) the argument is trimmed from whitespaces and new lines
|
||||
at the beginning and at the end
|
||||
(because otherwise a space would be changed to %20 and this were probably not what you really wanted)
|
||||
*/
|
||||
const BBCODEParser::Tags * BBCODEParser::FindTag(const char * tag)
|
||||
{
|
||||
static Tags tags[] = {
|
||||
{"*", "li", ">", false},
|
||||
{"b", "em", ">", true},
|
||||
{"i", "span", " class=\"bbitalic\">", true},
|
||||
{"u", "span", " class=\"bbunderline\">", true},
|
||||
{"s", "span", " class=\"bbstrike\">", true},
|
||||
{"code", "code", " class=\"bbcode\">", false},
|
||||
{"list", "ul", " class=\"bblist\">", false},
|
||||
{"color", "span", " class=\"bbcol%1\">", true},
|
||||
{"url", "a", " href=\"%u1\">", true},
|
||||
{"img", "img", " alt=\"%1\" src=\"%u2\">", true},
|
||||
{"quote", "div", " class=\"bbquote\">\n<span class=\"bbquotewho\">%1</span><br>\n", false},
|
||||
};
|
||||
|
||||
size_t i;
|
||||
size_t len = sizeof(tags) / sizeof(Tags);
|
||||
|
||||
for(i=0 ; i<len ; ++i)
|
||||
{
|
||||
if( strcmp(tag, tags[i].bbcode) == 0 )
|
||||
return &tags[i];
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void BBCODEParser::PrintArgumentCheckQuotes(const char * & start, const char * & end)
|
||||
{
|
||||
// skipping white characters from the argument
|
||||
while( start<end && IsWhite(*start) )
|
||||
++start;
|
||||
|
||||
// skipping first '=' character if exists
|
||||
if( start<end && *start == '=' )
|
||||
++start;
|
||||
|
||||
// skipping white characters from the argument
|
||||
// at the beginning
|
||||
while( start<end && IsWhite(*start) )
|
||||
++start;
|
||||
|
||||
// and at the end
|
||||
while( start<end && IsWhite(*(end-1)) )
|
||||
--end;
|
||||
|
||||
|
||||
if( start<end && (*start=='\'' || *start=='\"') )
|
||||
{
|
||||
++start;
|
||||
|
||||
if( start<end && *(start-1) == *(end-1) )
|
||||
--end;
|
||||
|
||||
// skipping white characters after a first quote char [url = " ww...."]
|
||||
while( start<end && IsWhite(*start) )
|
||||
++start;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void BBCODEParser::PrintEncode(int c)
|
||||
{
|
||||
if( c == '&' )
|
||||
{
|
||||
(*out_string) += "&";
|
||||
}
|
||||
else
|
||||
if( (c>='a' && c<='z') ||
|
||||
(c>='A' && c<='Z') ||
|
||||
(c>='0' && c<='9') ||
|
||||
(c=='_' || c=='?' || c=='.' || c==',' || c=='/' || c=='-' ||
|
||||
c=='+' || c=='*' || c=='(' || c==')' || c=='=' || c==':')
|
||||
)
|
||||
{
|
||||
(*out_string) += c;
|
||||
}
|
||||
else
|
||||
{
|
||||
char buffer[20];
|
||||
sprintf(buffer, "%02X", c);
|
||||
|
||||
(*out_string) += '%';
|
||||
(*out_string) += buffer;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void BBCODEParser::PrintEscape(int c, bool change_quote)
|
||||
{
|
||||
if( c == '<' )
|
||||
{
|
||||
(*out_string) += "<";
|
||||
}
|
||||
else
|
||||
if( c == '>' )
|
||||
{
|
||||
(*out_string) += ">";
|
||||
}
|
||||
else
|
||||
if( c == '&' )
|
||||
{
|
||||
(*out_string) += "&";
|
||||
}
|
||||
else
|
||||
if( c == '\"' && change_quote )
|
||||
{
|
||||
(*out_string) += """;
|
||||
}
|
||||
else
|
||||
{
|
||||
(*out_string) += c;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void BBCODEParser::PrintArgumentEncode(const char * start, const char * end)
|
||||
{
|
||||
PrintArgumentCheckQuotes(start, end);
|
||||
TrimWhiteWithNewLines(start, end);
|
||||
|
||||
for( ; start<end ; ++start )
|
||||
PrintEncode(*start);
|
||||
}
|
||||
|
||||
|
||||
void BBCODEParser::PrintArgumentEscape(const char * start, const char * end)
|
||||
{
|
||||
PrintArgumentCheckQuotes(start, end);
|
||||
|
||||
for( ; start<end ; ++start )
|
||||
PrintEscape(*start, true); // quotes are escaped as well here
|
||||
}
|
||||
|
||||
|
||||
void BBCODEParser::CheckOpeningTag(const Tags * tag, const char * tag_name, bool & condition)
|
||||
{
|
||||
if( strcmp(tag->html_tag, tag_name) == 0 )
|
||||
{
|
||||
if( condition )
|
||||
{
|
||||
PutClosingTag(tag);
|
||||
(*out_string) += '\n';
|
||||
}
|
||||
|
||||
condition = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void BBCODEParser::CheckOpeningTag(const Tags * tag)
|
||||
{
|
||||
bool has_list_tag = has_open_ul_tag || has_open_ol_tag;
|
||||
|
||||
CheckOpeningTag(tag, "li", has_open_li_tag);
|
||||
CheckOpeningTag(tag, "ul", has_open_ul_tag);
|
||||
CheckOpeningTag(tag, "ol", has_open_ol_tag);
|
||||
|
||||
if( has_open_li_tag && !has_list_tag )
|
||||
{
|
||||
(*out_string) += "<ul>\n";
|
||||
has_open_ul_tag = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void BBCODEParser::PrintEscape(const char * start, const char * end, bool change_quote)
|
||||
{
|
||||
for( ; start < end ; ++start)
|
||||
PrintEscape(*start, change_quote);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void BBCODEParser::PrintEncode(const char * start, const char * end)
|
||||
{
|
||||
for( ; start < end ; ++start)
|
||||
PrintEncode(*start);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void BBCODEParser::PutOpeningTagFromEzc(const char * start, const char * end)
|
||||
{
|
||||
// this can be a tag from Ezc templates system
|
||||
(*out_string) += '[';
|
||||
(*out_string) += LastItem().name;
|
||||
|
||||
if( start != end )
|
||||
{
|
||||
(*out_string) += ' ';
|
||||
PrintEscape(start, end);
|
||||
}
|
||||
|
||||
(*out_string) += ']';
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void BBCODEParser::PutHtmlArgument1(const char * arg_start, const char * arg_end, bool has_u)
|
||||
{
|
||||
if( has_u )
|
||||
PrintArgumentEncode(arg_start, arg_end);
|
||||
else
|
||||
PrintArgumentEscape(arg_start, arg_end);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void BBCODEParser::TrimWhiteWithNewLines(const char * & start, const char * & end)
|
||||
{
|
||||
while( start < end && (IsWhite(*start) || *start==10) )
|
||||
++start;
|
||||
|
||||
while( start < end && (IsWhite(*(end-1)) || *(end-1)==10) )
|
||||
--end;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void BBCODEParser::PutHtmlArgument2(const Tags * tag, bool has_u)
|
||||
{
|
||||
const char * start = pchar;
|
||||
const char * end = pchar;
|
||||
bool first_tag_removed = false;
|
||||
|
||||
while( *pchar != 0 )
|
||||
{
|
||||
if( IsOpeningTagMark() )
|
||||
{
|
||||
if( IsClosingTagForLastItem() )
|
||||
{
|
||||
// the last tag is skipped when using patterns with %2 or %u2
|
||||
|
||||
PopStack(); // removing opening tag from the stack
|
||||
first_tag_removed = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
pchar += 1;
|
||||
end = pchar;
|
||||
}
|
||||
}
|
||||
|
||||
if( !first_tag_removed )
|
||||
PopStack(); // user has forgotten to close the tag
|
||||
|
||||
if( has_u )
|
||||
{
|
||||
TrimWhiteWithNewLines(start, end);
|
||||
PrintEncode(start, end);
|
||||
}
|
||||
else
|
||||
{
|
||||
PrintEscape(start, end);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void BBCODEParser::PutHtmlArgument(const Tags * tag, const char * arg_start, const char * arg_end)
|
||||
{
|
||||
const char * pattern = tag->html_argument;
|
||||
bool has_u;
|
||||
|
||||
while( *pattern )
|
||||
{
|
||||
if( *pattern == '%' )
|
||||
{
|
||||
++pattern;
|
||||
has_u = false;
|
||||
|
||||
if( *pattern == 'u' )
|
||||
{
|
||||
++pattern;
|
||||
has_u = true;
|
||||
}
|
||||
|
||||
if( *pattern == '1' )
|
||||
{
|
||||
++pattern;
|
||||
PutHtmlArgument1(arg_start, arg_end, has_u);
|
||||
}
|
||||
else
|
||||
if( *pattern == '2' )
|
||||
{
|
||||
++pattern;
|
||||
PutHtmlArgument2(tag, has_u);
|
||||
}
|
||||
else
|
||||
if( *pattern == '%' )
|
||||
{
|
||||
(*out_string) += '%';
|
||||
++pattern;
|
||||
}
|
||||
// else unrecognized, will be printed next time as a normal character
|
||||
}
|
||||
else
|
||||
{
|
||||
(*out_string) += *pattern;
|
||||
++pattern;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void BBCODEParser::PutOpeningTagFromBBCode(const Tags * tag, const char * start, const char * end)
|
||||
{
|
||||
CheckOpeningTag(tag);
|
||||
PutOpeningTagMark();
|
||||
(*out_string) += tag->html_tag;
|
||||
PutHtmlArgument(tag, start, end);
|
||||
|
||||
if( !tag->inline_tag )
|
||||
{
|
||||
(*out_string) += "\n";
|
||||
SkipWhiteLines();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void BBCODEParser::PutOpeningTag(const char * start, const char * end)
|
||||
{
|
||||
const Tags * tag = FindTag(LastItem().name);
|
||||
|
||||
if( !tag )
|
||||
{
|
||||
PutOpeningTagFromEzc(start, end);
|
||||
}
|
||||
else
|
||||
{
|
||||
PutOpeningTagFromBBCode(tag, start, end);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void BBCODEParser::PutClosingTag(const Tags * tag)
|
||||
{
|
||||
if( !tag )
|
||||
return; // skipping the tag
|
||||
|
||||
PutOpeningTagMark();
|
||||
(*out_string) += '/';
|
||||
(*out_string) += tag->html_tag;
|
||||
PutClosingTagMark();
|
||||
|
||||
if( !tag->inline_tag )
|
||||
{
|
||||
(*out_string) += "\n";
|
||||
SkipWhiteLines();
|
||||
}
|
||||
|
||||
if( strcmp(tag->html_tag, "li") == 0 )
|
||||
has_open_li_tag = false;
|
||||
|
||||
if( strcmp(tag->html_tag, "ol") == 0 )
|
||||
has_open_ol_tag = false;
|
||||
|
||||
if( strcmp(tag->html_tag, "ul") == 0 )
|
||||
has_open_ul_tag = false;
|
||||
}
|
||||
|
||||
|
||||
void BBCODEParser::PutClosingTag(const char * tag_name)
|
||||
{
|
||||
const Tags * tag = FindTag(tag_name);
|
||||
PutClosingTag(tag);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void BBCODEParser::Init()
|
||||
{
|
||||
has_open_li_tag = false;
|
||||
has_open_ol_tag = false;
|
||||
has_open_ul_tag = false;
|
||||
|
||||
SkipWhiteLines();
|
||||
}
|
||||
|
||||
|
||||
void BBCODEParser::Deinit()
|
||||
{
|
||||
if( has_open_li_tag )
|
||||
(*out_string) += "</li>\n";
|
||||
|
||||
if( has_open_ol_tag )
|
||||
(*out_string) += "</ol>\n";
|
||||
|
||||
if( has_open_ul_tag )
|
||||
(*out_string) += "</ul>\n";
|
||||
}
|
@ -0,0 +1,95 @@
|
||||
/*
|
||||
* This file is a part of Winix
|
||||
* and is not publicly distributed
|
||||
*
|
||||
* Copyright (c) 2008-2010, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef headerfilecmslucorebbcodeparser
|
||||
#define headerfilecmslucorebbcodeparser
|
||||
|
||||
#include "htmlfilter.h"
|
||||
|
||||
|
||||
class BBCODEParser : public HTMLFilter
|
||||
{
|
||||
|
||||
//using HTMLFilter::pchar;
|
||||
|
||||
|
||||
struct Tags
|
||||
{
|
||||
/*
|
||||
const char * bbcode;
|
||||
const char * html_tag;
|
||||
const char * html_arg_prefix;
|
||||
const char * html_arg_postfix;
|
||||
const char * additional_html_tag_prefix;
|
||||
const char * additional_html_tag_postfix;
|
||||
bool inline_tag;
|
||||
*/
|
||||
const char * bbcode;
|
||||
const char * html_tag;
|
||||
const char * html_argument; // with closing '>'
|
||||
bool inline_tag;
|
||||
};
|
||||
|
||||
virtual bool IsValidCharForName(int c);
|
||||
|
||||
virtual bool IsOpeningTagMark();
|
||||
virtual bool IsOpeningCommentaryTagMark();
|
||||
virtual bool SkipCommentaryTagIfExists();
|
||||
virtual bool IsClosingTagMark();
|
||||
virtual bool IsClosingXmlSimpleTagMark();
|
||||
|
||||
|
||||
void PutHtmlArgument1(const char * arg_start, const char * arg_end, bool has_u);
|
||||
void PutHtmlArgument2(const Tags * tag, bool has_u);
|
||||
void PutHtmlArgument(const Tags * tag, const char * arg_start, const char * arg_end);
|
||||
|
||||
void PutOpeningTagFromEzc(const char * start, const char * end);
|
||||
void PutOpeningTagFromBBCode(const Tags * tag, const char * start, const char * end);
|
||||
|
||||
virtual void PutOpeningTag(const char * start, const char * end);
|
||||
virtual void PutClosingTag(const char * tag);
|
||||
|
||||
const Tags * FindTag(const char * tag);
|
||||
void PrintArgumentCheckQuotes(const char * & start, const char * & end);
|
||||
|
||||
|
||||
void PrintEscape(int c, bool change_quote = false);
|
||||
void PrintEncode(int c);
|
||||
|
||||
void PrintEscape(const char * start, const char * end, bool change_quote = false);
|
||||
void PrintEncode(const char * start, const char * end);
|
||||
|
||||
void PrintArgumentEncode(const char * start, const char * end);
|
||||
void PrintArgumentEscape(const char * start, const char * end);
|
||||
|
||||
virtual void ReadNormalTextSkipWhite(const char * & start, const char * & last_non_white);
|
||||
virtual void PutNormalText(const char * str, const char * end);
|
||||
virtual void PutNormalTextTrim(const char * str, const char * end);
|
||||
|
||||
virtual void CheckExceptions();
|
||||
|
||||
virtual void Init();
|
||||
virtual void Deinit();
|
||||
|
||||
void PutClosingTag(const Tags * tag);
|
||||
|
||||
|
||||
void CheckOpeningTag(const Tags * tag, const char * tag_name, bool & condition);
|
||||
void CheckOpeningTag(const Tags * tag);
|
||||
|
||||
void TrimWhiteWithNewLines(const char * & start, const char * & end);
|
||||
|
||||
bool has_open_ol_tag; // has open html <ol> tag
|
||||
bool has_open_ul_tag; // has open html <ul> tag
|
||||
bool has_open_li_tag; // has open html <li> tag
|
||||
|
||||
|
||||
};
|
||||
|
||||
#endif
|
Loading…
Reference in new issue