博客首页 注册 建议与交流 排行榜 加入友情链接
推荐 投诉 搜索: 帮助

hope_process

感觉好累。。。
  heixia108.cublog.cn

关于作者
    既然目标是地平线

   留给世界的只能是背影
   
|| << >> ||
我的分类


.cpp文件 to html 简易版 ( for sea )
 能对cpp文件进行简单的处理,进行关健字加亮.
 
txt2html.h

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <map>

const int indent_size = 4;

class txt2html
{
public:
    txt2html(){
        init();
        indent = 0;
    }

    void    read_file(std::string file);
    void    tohtml();
private:
    void    init();

    std::vector< std::string >    lines_of_txt; // 存储文件的每一行

    std::map< std::string,std::string > word_map; //把从word_font.map文件中读取与关健字对应的颜色值存入word_ma

    std::string filename; //如果文件名为input.cpp ,则置filename = "input.html";

    int        indent;     //用来记缩进的,遇到 "{"加1,遇到"}" 减 1

};

txt2html.cpp

// txt2html.cpp: implementation of the txt2html class.

//

//////////////////////////////////////////////////////////////////////


#include "txt2html.h"
#include <algorithm>

using namespace std;

/*初始化: 从word_font.map文件中读取与关健字对应的颜色值存入word_map中
 *例: word_font.map中第二行为:
 * using    blue
 * 则 word_map ["using"] = "blue";
 * 这样在cpp文件中遇到using 关健字时,就可以用 <font color="blue">using</font>替换
 */

void    txt2html::init(){
    ifstream map_file( "word_font.map" );
    string word,font;

    if( !map_file )    {
        cerr << "error : unable to open the initial file : word_font.map" << endl;
        exit(1);
    }

    while( map_file >> word >> font )    {
        word_map.insert( make_pair( word,font ) );
        
        cout << word << " : " << font << endl;
    }

    cout << " Inital successfully..............." << endl;
}

//读入待转换的cpp文件

void txt2html::read_file(std::string file){
    ifstream is(file.c_str());
    string textline;

    //例:如果文件名为input.cpp ,则置filename = "input.html";

    string::size_type it = file.find_last_of(".");
    
    if( it != string::npos )
        filename.assign( file.begin(),file.begin() + it);
    else
        filename.assign( file.begin(),file.end() );

    filename.append(".html");

    if( !is ){
        cerr << "error : unable to creat the input file : " << file << endl;
        exit(1);
    }

    cout << "reading .................." << endl;

    while( getline( is,textline ) ) {
         string::size_type st = textline.find("<");
        
         while (st != textline.npos) { //将 "<" 替换为 html 中的 "&lt"

            textline.replace( st,1,"&lt");
            st = textline.find_first_of("<");
         }

         st = textline.find(">");
        
         while( st != textline.npos) { //将 ">" 替换为 html 中的 "&gt"

            textline.replace( st,1,"&gt");
            st = textline.find_first_of(">");
         }
        
         /*
         * 想实现上面同样的替换功能,但失败了
         replace( textline.begin(),textline.end() , "<", "&lt" );
         replace( textline.begin(),textline.end() , ">", "&gt" );
         */


         lines_of_txt.push_back( textline ); //将文件中的每一行用vector容器存起来

         cout << textline << endl;
    }
}

//转换为html

void txt2html::tohtml(){
    ofstream html( filename.c_str() );

    if( !html ){
        cerr << "error : unable to creat the output file : " << html << endl;
        exit(1);
    }

    html << "<div class=\"cpp\">" ; //html 中的文件开始行

    
    vector<string>::size_type size = lines_of_txt.size();

    cout << "size : " << size << endl;

    for( vector<string>::size_type i = 0 ; i < size ; i ++)    { //取出存储文件的容器中的每一行

        istringstream    line(lines_of_txt[i]);
        string word;
        int num = 1;

        if( line >> word ) {
            int n = indent;
            
            /*
                每一行开头进行缩进
                如果遇到"}"不进行 n-- ,那么打印出的 "}" 缩进不好看,如下:
                {
                    int c;
                    }
            */

            if( word == "{")
                indent ++;
            else if( word == "}") {
                indent --;
                n --;
            }

            for( int i = 0 ; i < n ; i ++ )
                for( int j = 0 ; j < indent_size ; j ++ )
                    html << "&nbsp";
    
            do
            {
                cout << word << endl;
                    
                if( num > 1 ) {
                    if( word == "{")
                        indent ++;
                    else if( word == "}") {
                        indent --;
                        n --;
                    }
                }

                num ++;

                map<string,string>::iterator it = word_map.find(word);
            
                if( it != word_map.end() )    {
                    html << "<font color=";
                    html << "\"";
                    html << it->second;
                    html << "\">";
                    html << word;

                    if(    word != "#include")    {    
                        html << "</font>";
                    }
                    else{
                        while( line >> word )
                            html << word;
                    html << "</font>";
                    }
                    
                }
                else
                    html << word;

                html << " ";
            }while( line >> word );
            html << "<br>";
        }
        else
            html << "<br>";
    }
    html << "</div>"; //html 中的文件结束行

}


 

文件: cpp2html.rar
大小: 2KB
下载: 下载

 

注:

javascript 可以参考: http://www.bedaux.net/cpp2html/
http://hi.baidu.com/flyskymlf/blog/item/79dfe2a2a4e0e7aecbefd0e5.html

 

还有一个用lex 写的: 一个用于cu论坛C语言代码语法高亮的小工具

发表于: 2008-07-06,修改于: 2008-07-07 00:07,已浏览97次,有评论0条 推荐 投诉


网友评论
 发表评论