Turbo-C
C++Builder  |  Delphi  |  FireMonkey  |  C/C++  |  Free Pascal  |  Firebird
볼랜드포럼 BorlandForum
 경고! 게시물 작성자의 사전 허락없는 메일주소 추출행위 절대 금지
터보-C 포럼
Q & A
FAQ
팁&트릭
강좌/문서
자료실
Lua 게시판
볼랜드포럼 홈
헤드라인 뉴스
IT 뉴스
공지사항
자유게시판
해피 브레이크
공동 프로젝트
구인/구직
회원 장터
건의사항
운영진 게시판
회원 메뉴
북마크
볼랜드포럼 광고 모집

C/C++ 팁&트릭
[36] [Boost]Regex++(2): C++ 소스 코드를 HTML로 변환
김백일 [cedar] 56362 읽음    2002-08-24 14:34
c2html.exe 395.5KB C++Builder 6로 컴파일한 실행 파일
c2html.cpp

Boost Library의 Regex++의 예제 중 하나입니다.

boost::regex_merge를 사용하여, C++ 소스 코드를 문법 강조(Syntax Highlighting)된 형태의 HTML로 변환해줍니다.

IDE나 에디터에서 외부 프로그램으로 등록시켜서 사용해도 좋고, 웹 프로그래밍하시는 분들은 CGI에 응용하셔도 좋겠네요.

 
/*
 *
 * 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 <boost/version.hpp>
  *   DESCRIPTION: regex_merge example:
  *                converts a C++ file to syntax highlighted HTML.
  */
 
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <iterator>
#include <boost/regex.hpp>
 
// 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<font color=\"#008040\">$&</font>)"
  "(?2<I><font color=\"#000080\">$&</font></I>)"
  "(?3<font color=\"#00A0A0\">$&</font>)"
  "(?4<font color=\"#0000FF\">$&</font>)"
  "(?5<B>$&</B>)"
  "(?6<font color=\"#800080\">$&</font>)",
 
*header_text1 = "<HTML>\n<HEAD>\n<TITLE>",
*header_text2 = "</TITLE>\n"
  "<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=euc-kr\">\n"
  "</HEAD>\n<BODY LINK=\"#0000ff\" VLINK=\"#800080\" BGCOLOR=\"#ffffff\">\n"
  "<P> </P>\n<PRE>",
*footer_text = "</PRE>\n</BODY>\n</HTML>";
 
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<char>(fin)),
                     istreambuf_iterator<char>());
          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<char> 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<char> 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;
}
명진 [killrain]   2006-01-22 11:48 X
헛.. 벌써 만들어져 있네요..
아랜 제가 만든 php버전입니다.
http://www.killrain.net/tt/index.php?pl=7

+ -

관련 글 리스트
36 [Boost]Regex++(2): C++ 소스 코드를 HTML로 변환 김백일 56362 2002/08/24
Google
Copyright © 1999-2015, borlandforum.com. All right reserved.