bbcodelib 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,159 @@
1
+ /*!
2
+
3
+ Abstract:
4
+ this file is a part of bbcode library
5
+
6
+ Author:
7
+ Igor Franchuk (sprog@online.ru)
8
+
9
+ Last Update:
10
+ $Id: bbcode_parser.h,v 1.1 2007/12/19 19:13:30 lanthruster Exp $
11
+ Version: 0.01
12
+ */
13
+
14
+ #ifndef BBCODE_parseR_H
15
+ #define BBCODE_parseR_H
16
+ #pragma once
17
+
18
+ #include <string>
19
+ #include <iostream>
20
+ #include <istream>
21
+ #include <stdexcept>
22
+ #include <stack>
23
+ #include <vector>
24
+ #include <map>
25
+
26
+ #include "bbcode_config.h"
27
+ #include "bbcode_lexer.h"
28
+ #include "bbcode_utils.h"
29
+
30
+ #pragma warning(disable: 4290) //VS doesn't support exception specifications
31
+
32
+ namespace{
33
+
34
+ using std::string;
35
+ using std::istream;
36
+ using std::cin;
37
+ using std::out_of_range;
38
+ using std::runtime_error;
39
+ using std::stack;
40
+ using std::vector;
41
+ using std::map;
42
+ using std::make_pair;
43
+ }
44
+
45
+ namespace bbcode{
46
+
47
+ //--------------------------------
48
+ // tag struct
49
+ struct tag{
50
+ enum tag_type{opening, closing};
51
+ typedef map<string, string> attribs;
52
+
53
+ string name;
54
+ tag_type type;
55
+ size_t left_bracket_pos;
56
+ size_t right_bracket_pos;
57
+ attribs attr;
58
+ bool has_closing_tag;
59
+
60
+ void clear(void){name = "", type = opening, left_bracket_pos = right_bracket_pos = 0; attr.clear();has_closing_tag = true;}
61
+ template<class T>
62
+ bool operator ==(T _t){if(name == _t.name && type == _t.type && left_bracket_pos == _t.left_bracket_pos &&
63
+ right_bracket_pos == _t.right_bracket_pos && attr == _t.attr && has_closing_tag == _t.has_closing_tag) return true;
64
+ return false;
65
+ }
66
+
67
+ tag(void):
68
+ name(""),
69
+ type(opening),
70
+ left_bracket_pos(0),
71
+ right_bracket_pos(0),
72
+ has_closing_tag(true)
73
+ {}
74
+ };
75
+ //--------------------------------
76
+ // this is multipart form data parser class
77
+
78
+ class parser{
79
+
80
+ typedef vector<tag> tags;
81
+ typedef map<string, string> supported_tag_storage;
82
+
83
+ parser_config _pconf; /* we keep all our configuration in this structure */
84
+ lexer _lexer; /* lexer, the source of lexems */
85
+ conds _current_state;/* parser's current state, defined by cond table */
86
+ stack<lexeme> _lexems;
87
+ string _parsed_data; /* used to keep immediate parsed data */
88
+ lexeme l;
89
+ string _im_data;
90
+
91
+ supported_tag_storage _supported_tag_storage; //preloaded tags that we support in BBcode library
92
+
93
+ tags _parsed_tags;
94
+ tag _tmp_tag;
95
+
96
+ void _bbcode_replacements(void); //here we're replacing bbcode tags
97
+ string _tag_replacement(tag& t); //replace bb tag with HTML tag
98
+ tags _discarded_tags;//an internal unclosed|overlapped|sentencedtodleletion tag storage, required for U|O recovery
99
+ void _discarded_tags_cleanup(void);
100
+
101
+ string _js_guard(string str); //this is a simple javascript: insert protection
102
+
103
+ public:
104
+ enum flag{store_data_in_memory=0, store_data_in_file=1};
105
+
106
+ //tag schem loader
107
+
108
+ void load_supported_tag_schema(const string& schema);
109
+
110
+ string current_tag_schema(void) const;
111
+
112
+ /* class configuration block */
113
+
114
+ size_t setf(size_t mode){ size_t old_conf = _pconf._config; _pconf._config |= mode; return old_conf; }
115
+
116
+ void source_stream(istream& source_stream){ _pconf._source_stream = &source_stream; }
117
+ istream* source_stream(void) const{ return _pconf._source_stream; }
118
+
119
+ size_t read_buffer_size(size_t buffer_size) throw(out_of_range);
120
+
121
+ size_t read_buffer_size( void ) const { return _pconf._read_buffer_size; }
122
+
123
+ parser_config pconf(parser_config& pconf){ parser_config tconf = _pconf; _pconf = pconf; return tconf;}
124
+ parser_config pconf(void) const { return _pconf;}
125
+
126
+
127
+ /* end of class configuration block */
128
+
129
+ /* class functinality */
130
+
131
+ void parse(void);
132
+ /* return alternative multipart form content */
133
+ string content(void) const { return _parsed_data; }
134
+
135
+ /* class configuration block */
136
+
137
+ /* constructors */
138
+
139
+ parser(void) : _pconf(cin, _DEFAULT_BUFFER_SIZE),
140
+ _current_state(C1),
141
+ l(TERM),
142
+ _im_data(""),
143
+ _parsed_data("")
144
+ {load_supported_tag_schema(_SUPPORTED_BBTAGS);}
145
+
146
+ parser(const string& schema) : _pconf(cin, _DEFAULT_BUFFER_SIZE),
147
+ _current_state(C1),
148
+ l(TERM),
149
+ _im_data(""),
150
+ _parsed_data("")
151
+ {load_supported_tag_schema(schema);}
152
+ /* end of constructors */
153
+
154
+ virtual ~parser(){};
155
+ };
156
+
157
+ }
158
+
159
+ #endif
@@ -0,0 +1,143 @@
1
+ #include "bbcode_utils.h"
2
+
3
+ namespace bbcode{
4
+
5
+ //--------------------------------
6
+ // upper case block
7
+ string upper_case(string s){
8
+ transform(s.begin(), s.end(), s.begin(), capitalize);
9
+ return s;
10
+ }
11
+ unsigned char capitalize(unsigned char c){
12
+ return toupper(c);
13
+ }
14
+ //--------------------------------
15
+ // form_parcer, helper functions
16
+
17
+ string get_module_msg(const string& str){
18
+ return _THIS_MODULE_STR_IDENT + " " + str;
19
+ }
20
+
21
+ //--------------------------------
22
+ // form_parcer, helper functions
23
+ string random_string(size_t length){
24
+ string res;
25
+ string dictionary = _RND_GENERATOR_DICTIONARY;
26
+ if(dictionary.empty()) throw runtime_error(get_module_msg("Empty _RND_GENERATOR_DICTIONARY"));
27
+ for(size_t i = 0; i < length; i++){
28
+ random_shuffle(dictionary.begin(), dictionary.end());
29
+ //res.push_back(dictionary[0]);
30
+ res.append(1, dictionary[0]);
31
+ }
32
+ return res;
33
+ }
34
+
35
+ // ------------------------------------------------------------------------------------------------------------------
36
+ //! Templates specializations
37
+ // ------------------------------------------------------------------------------------------------------------------
38
+
39
+ // ------------------------------------------------------------------------------------------------------------------
40
+ //! string to string spec (two arg)
41
+ template<> string stream_cast(const string &value, cast_manip manip)
42
+ {
43
+ return value;
44
+ }
45
+
46
+ // ------------------------------------------------------------------------------------------------------------------
47
+ //! char* to string spec (two arg)
48
+ template<> string stream_cast(const char* value, cast_manip manip)
49
+ {
50
+ return string(value);
51
+ }
52
+
53
+ // ------------------------------------------------------------------------------------------------------------------
54
+ //! char* to string spec (one arg)
55
+ template<> string stream_cast(const string &value)
56
+ {
57
+ return value;
58
+ }
59
+
60
+ // ------------------------------------------------------------------------------------------------------------------
61
+ //! char* to string spec (one arg)
62
+ template<> string stream_cast(const char* value)
63
+ {
64
+ return string(value);
65
+ }
66
+
67
+ // ------------------------------------------------------------------------------------------------------------------
68
+ //! Split string
69
+ strings split_string(const string &str, const string &delimiter, const bool include_empty)
70
+ {
71
+ strings result;
72
+ string sub = "";
73
+ std::size_t dpos = 0, lastdpos = 0, dsize = delimiter.size();
74
+ do {
75
+ dpos = str.find(delimiter, lastdpos);
76
+ sub = str.substr(lastdpos, dpos - lastdpos);
77
+ if (!sub.empty() || include_empty) result.push_back(sub);
78
+ lastdpos = dpos + dsize;
79
+ } while (dpos != string::npos);
80
+ return result;
81
+ }
82
+
83
+ // ------------------------------------------------------------------------------------------------------------------
84
+ //! Splits string so that length of any substring is no more than max_sub_length
85
+ strings split_string(const string &str, const string &delimiter, bool include_empty, size_t max_sub_length)
86
+ {
87
+ strings result = split_string(str, delimiter, include_empty);
88
+ if (max_sub_length > 0) {
89
+ strings new_res;
90
+ for (strings::iterator it = result.begin(); it != result.end(); it++) {
91
+ size_t len = it->length();
92
+ if (len > max_sub_length) {
93
+ size_t count = len / max_sub_length + 1;
94
+ strings subsplit;
95
+ for (size_t i = 0; i < count; i++) {
96
+ subsplit.push_back(it->substr(i * max_sub_length, max_sub_length));
97
+ }
98
+ new_res.insert(new_res.end(), subsplit.begin(), subsplit.end());
99
+ }
100
+ else {
101
+ new_res.push_back(*it);
102
+ }
103
+ }
104
+ return new_res;
105
+ }
106
+ return result;
107
+ }
108
+ //----------------------------------------------------------
109
+ //! functor for find_if algorithm
110
+ //! trim_left, trim_right
111
+ class trim_findfunct {
112
+ const string _char_list;
113
+ public:
114
+ trim_findfunct(const string char_list):_char_list(char_list) { }
115
+ bool operator () ( const char ch) const {
116
+ return string::npos == _char_list.find(ch);
117
+ }
118
+ };
119
+ //----------------------------------------------------------
120
+ //! erase head spaces
121
+ string trim_left( const string &str, const string &char_list ) {
122
+ string ret = "";
123
+ string::const_iterator it = find_if(str.begin(), str.end(), trim_findfunct(char_list) );
124
+ copy( it, str.end(), back_inserter(ret) );
125
+ return ret;
126
+ }
127
+
128
+ //----------------------------------------------------------
129
+ //! erase trail spaces
130
+ string trim_right( const string &str, const string &char_list ) {
131
+ string ret = "";
132
+ string::const_reverse_iterator it = find_if(str.rbegin(), str.rend(), trim_findfunct(char_list) );
133
+ copy(str.begin(), str.begin() + (str.rend() - it) , back_inserter(ret));
134
+ return ret;
135
+ }
136
+
137
+ //----------------------------------------------------------
138
+ //! erase head and trail spaces
139
+ string trim( const string &str, const string &char_list ) {
140
+ return trim_left(trim_right(str,char_list),char_list);
141
+ }
142
+
143
+ }
@@ -0,0 +1,144 @@
1
+ /*!
2
+ Abstract:
3
+ this file is a part of bbcode library
4
+
5
+ Author:
6
+ Igor Franchuk (sprog@online.ru)
7
+
8
+ Last Update:
9
+ $Id: bbcode_utils.h,v 1.1 2007/12/19 19:13:30 lanthruster Exp $
10
+ Version: 0.01
11
+ */
12
+
13
+ #ifndef BBCODE_UTILS_H
14
+ #define BBCODE_UTILS_H
15
+
16
+ #include <string>
17
+ #include <vector>
18
+ #include <sstream>
19
+ #include <istream>
20
+ #include <iostream>
21
+ #include <algorithm>
22
+ #include <functional>
23
+ #include <time.h>
24
+ #include <stdexcept>
25
+ #include "bbcode_config.h"
26
+
27
+ namespace{
28
+ using std::string;
29
+ using std::istream;
30
+ using std::transform;
31
+ using std::uppercase;
32
+ using std::cin;
33
+ using std::cout;
34
+ using std::stringstream;
35
+ using std::random_shuffle;
36
+ using std::runtime_error;
37
+ using std::vector;
38
+ using std::find_if;
39
+ }
40
+
41
+ namespace bbcode{
42
+ typedef vector<string> strings;
43
+ //--------------------------------
44
+ // helper functions
45
+ unsigned char capitalize(unsigned char c);
46
+ string upper_case(string s); /* only for Latin-1, locale is not used */
47
+ string random_string(size_t length); /* returns random string sequence */
48
+
49
+ //--------------------------------
50
+ // helper templates
51
+
52
+ template<class T, class C>
53
+ T myswap(T& p1, C& p2){
54
+ T old = p1;
55
+ p1 = p2;
56
+ return old;
57
+ };
58
+
59
+ //! Type T to Q std stream conversion
60
+ enum cast_manip { cm_none, cm_dec, cm_hex, cm_oct, cm_boolalpha };
61
+
62
+ //! Cast implementation
63
+ template<class T, class Q>
64
+ inline T stream_cast_impl(const Q &value, cast_manip manip)
65
+ {
66
+ std::stringstream buf;
67
+ T res;
68
+ switch (manip)
69
+ {
70
+ case cm_dec: buf << std::dec; break;
71
+ case cm_hex: buf << std::hex; break;
72
+ case cm_oct: buf << std::oct; break;
73
+ case cm_boolalpha: buf << std::boolalpha; break;
74
+ }
75
+ buf << value;
76
+ buf >> res;
77
+ return res;
78
+ }
79
+
80
+ //! For cast with two arguments
81
+ template<class T, class Q>
82
+ T stream_cast(const Q &value, cast_manip manip)
83
+ {
84
+ return stream_cast_impl<T>(value, manip);
85
+ }
86
+
87
+ //! char* spec (two arg)
88
+ template<class T> T stream_cast(const char* value, cast_manip manip)
89
+ {
90
+ return stream_cast_impl<T>(string(value), manip);
91
+ }
92
+ //! For one argument casting
93
+ template<class T, class Q> T stream_cast(const Q &value)
94
+ {
95
+ return stream_cast_impl<T>(value, cm_none);
96
+ }
97
+ //! char* spec (one arg)
98
+ template<class T> T stream_cast(const char* value)
99
+ {
100
+ return stream_cast_impl<T>(string(value));
101
+ }
102
+
103
+ //! size_t hack for WIN32
104
+ #ifdef WIN32
105
+ //! size_t spec (one arg)
106
+ template<class T> T stream_cast(const size_t value)
107
+ {
108
+ return stream_cast_impl<T>(static_cast<unsigned int>(value), cm_none);
109
+ }
110
+ //! size_t spec (one arg)
111
+ template<class T> T stream_cast(const size_t value, cast_manip manip)
112
+ {
113
+ return stream_cast_impl<T>(static_cast<unsigned int>(value), manip);
114
+ }
115
+ #endif
116
+
117
+ //! string to string spec (two arg)
118
+ template<> string stream_cast(const string &value, cast_manip manip);
119
+ //! string to string spec (one arg)
120
+ template<> string stream_cast(const string &value);
121
+ //! char* to string spec (two arg)
122
+ template<> string stream_cast(const char* value, cast_manip manip);
123
+ //! char* to string spec (one arg)
124
+ template<> string stream_cast(const char* value);
125
+
126
+ //--------------------------------
127
+ // helper functions
128
+
129
+ string get_module_msg(const string& str);
130
+
131
+ //! Splits string
132
+ strings split_string(const string &str, const string &delimiter, const bool include_empty = true);
133
+ //! Splits string so that length of any substring is no more than max_sub_length
134
+ strings split_string(const string &str, const string &delimiter, bool include_empty, size_t max_sub_length);
135
+ //! erase head spaces
136
+ string trim_left( const string &str, const string &char_list=" \t\n\r\0\x0B");
137
+ //! erase trail spaces
138
+ string trim_right( const string &str, const string &char_list=" \t\n\r\0\x0B" );
139
+ //! erase head and trail spaces
140
+ string trim( const string &str, const string &char_list=" \t\n\r\0\x0B" );
141
+
142
+
143
+ }
144
+ #endif
data/ext/extconf.rb ADDED
@@ -0,0 +1,13 @@
1
+ # Loads mkmf which is used to make makefiles for Ruby extensions
2
+ require 'mkmf'
3
+
4
+ # Give it a name
5
+ extension_name = 'bbcode'
6
+
7
+ # The destination
8
+ dir_config(extension_name)
9
+
10
+ have_library("stdc++")
11
+
12
+ # Do the work
13
+ create_makefile(extension_name)
data/lib/bbcodelib.rb ADDED
@@ -0,0 +1,6 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ module Bbcodelib
5
+ VERSION = '1.0.0'
6
+ end
data/script/console ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # File: script/console
3
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
4
+
5
+ libs = " -r irb/completion"
6
+ # Perhaps use a console_lib to store any extra methods I may want available in the cosole
7
+ # libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
8
+ libs << " -r #{File.dirname(__FILE__) + '/../lib/bbcodelib.rb'}"
9
+ puts "Loading bbcodelib gem"
10
+ exec "#{irb} #{libs} --simple-prompt"
data/script/destroy ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/destroy'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Destroy.new.run(ARGV)
data/script/generate ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/generate'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Generate.new.run(ARGV)
@@ -0,0 +1,11 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestBbcodelib < Test::Unit::TestCase
4
+
5
+ def setup
6
+ end
7
+
8
+ def test_truth
9
+ assert true
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ require 'stringio'
2
+ require 'test/unit'
3
+ require File.dirname(__FILE__) + '/../lib/bbcodelib'
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bbcodelib
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Stephen Blackstone
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-09-29 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hoe
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 2.3.3
24
+ version:
25
+ description: Provides a fast, efficent BBcode parser for ruby via the bbcodelib package at sourceforge
26
+ email:
27
+ - sblackstone@gmail.com
28
+ executables: []
29
+
30
+ extensions:
31
+ - ext/extconf.rb
32
+ extra_rdoc_files:
33
+ - History.txt
34
+ - Manifest.txt
35
+ files:
36
+ - History.txt
37
+ - Manifest.txt
38
+ - README.rdoc
39
+ - Rakefile
40
+ - bbcodelib.gemspec
41
+ - ext/Makefile
42
+ - ext/bbcode.cpp
43
+ - ext/bbcode_config.h
44
+ - ext/bbcode_lexer.cpp
45
+ - ext/bbcode_lexer.h
46
+ - ext/bbcode_parser.cpp
47
+ - ext/bbcode_parser.h
48
+ - ext/bbcode_utils.cpp
49
+ - ext/bbcode_utils.h
50
+ - ext/extconf.rb
51
+ - lib/bbcodelib.rb
52
+ - script/console
53
+ - script/destroy
54
+ - script/generate
55
+ - test/test_bbcodelib.rb
56
+ - test/test_helper.rb
57
+ has_rdoc: true
58
+ homepage: http://rubyforge.org/projects/bbcodelib/
59
+ licenses: []
60
+
61
+ post_install_message: PostInstall.txt
62
+ rdoc_options:
63
+ - --main
64
+ - README.rdoc
65
+ require_paths:
66
+ - lib
67
+ - ext
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: "0"
73
+ version:
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: "0"
79
+ version:
80
+ requirements: []
81
+
82
+ rubyforge_project: bbcodelib
83
+ rubygems_version: 1.3.5
84
+ signing_key:
85
+ specification_version: 3
86
+ summary: A ruby binding for the bbcodelib library (sourceforge)
87
+ test_files:
88
+ - test/test_bbcodelib.rb
89
+ - test/test_helper.rb