/* * * Copyright (c) 1998-2002 * Dr John Maddock * * Permission to use, copy, modify, distribute and sell this software * and its documentation for any purpose is hereby granted without fee, * provided that the above copyright notice appear in all copies and * that both that copyright notice and this permission notice appear * in supporting documentation. Dr John Maddock makes no representations * about the suitability of this software for any purpose. * It is provided "as is" without express or implied warranty. * */ /* * LOCATION: see http://www.boost.org for most recent version. * FILE: regex_merge_example.cpp * VERSION: see * DESCRIPTION: regex_merge example: * converts a C++ file to syntax highlighted HTML. */ #include #include #include #include #include #include #include // purpose: // takes the contents of a file and transform to // syntax highlighted code in html format const char *pre_expression = "(<)|(>)|\\r", *pre_format = "(?1<)(?2>)", *expression_text = // index 1: preprocessor directives "(^[[:blank:]]*#(?:[^\\\\\\n]|\\\\[^\\n[:punct:][:word:]]*[\\n[:punct:][:word:]])*)|" // index 2: comment "(//[^\\n]*|/\\*.*?\\*/)|" // index 3: number literals "\\<([+-]?(?:(?:0x[[:xdigit:]]+)|(?:(?:[[:digit:]]*\\.)?[[:digit:]]+(?:[eE][+-]?[[:digit:]]+)?))u?(?:(?:int(?:8|16|32|64))|L)?)\\>|" // index 4: string literals "('(?:[^\\\\']|\\\\.)*'|\"(?:[^\\\\\"]|\\\\.)*\")|" // index 5: keywords "\\<(__asm|__cdecl|__declspec|__export|__far16|__fastcall|__fortran|__import" "|__pascal|__rtti|__stdcall|_asm|_cdecl|__except|_export|_far16|_fastcall" "|__finally|_fortran|_import|_pascal|_stdcall|__thread|__try|asm|auto|bool" "|break|case|catch|cdecl|char|class|const|const_cast|continue|default|delete" "|do|double|dynamic_cast|else|enum|explicit|extern|false|float|for|friend|goto" "|if|inline|int|long|mutable|namespace|new|operator|pascal|private|protected" "|public|register|reinterpret_cast|return|short|signed|sizeof|static|static_cast" "|struct|switch|template|this|throw|true|try|typedef|typeid|typename|union|unsigned" "|using|virtual|void|volatile|wchar_t|while)\\>|" // index 6: symbol "([[:punct:]]|<|>)", *format_string = "(?1$&)" "(?2$&)" "(?3$&)" "(?4$&)" "(?5$&)" "(?6$&)", *header_text1 = "\n\n", *header_text2 = "\n" "\n" "\n\n" "

\n
",
*footer_text = "
\n\n"; using namespace std; int main(int argc, const char** argv) { try{ boost::regex e1(expression_text), e2(pre_expression); for(int i = 1; i < argc; ++i) { cout << "Processing file " << argv[i] << endl; ifstream fin(argv[i]); string in((istreambuf_iterator(fin)), istreambuf_iterator()); string out_name(argv[i]); out_name.append(".htm"); ofstream fout(out_name.c_str()); fout << header_text1 << argv[i] << header_text2; // strip '<' and '>' first by outputting to a // temporary string stream ostringstream t(ios::out | ios::binary); ostream_iterator oi(t); boost::regex_merge(oi, in.begin(), in.end(), e2, pre_format); // then output to final output stream // adding syntax highlighting: string s(t.str()); ostream_iterator out(fout); boost::regex_merge(out, s.begin(), s.end(), e1, format_string); fout << footer_text; fout.close(); system(out_name.c_str()); } } catch(...) { return -1; } return 0; }