python实现:https://github.com/captainwong/instant_markup

c++实现:https://github.com/captainwong/instant_markup_cpp

要点:

1.标准输入输出流的重定向

python markup.py < test_input.txt > test_output.html

上述命令将标准输入设备重定向为文件input.txt,将标准输出设备重定向为文件test_output.html。

Python中使用的标准输入设备为sys.stdin, 输出使用函数print。C语言使用stdin和函数printf等。c++使用cin。 cout。

2.使用字符串调用函数

如依据字符串"foo"查找函数foo并调用之。

def callback(self, prefix, name, *args):
method = getattr(self, prefix+name, None)
if callable(method): return method(*args) def start(self, name):
self.callback('start_', name) def end(self, name):
self.callback('end_', name) def sub(self, name):
def substitution(match):
result = self.callback('sub_', name, match)
if result is None: match.group(0)
return result
return substitution

使用时可通过调用

start('document')

来调用start_document函数。

c++无此特性。这里我使用map保存函数名和函数指针的方法来模拟这样的功能。

首先定义函数指针:

typedef void (CHandler::*pFunc)();

定义静态map成员:

static map<string, pFunc> m_funcmap;

使用宏定义简化初始化操作:

#define STR(str) #str
#define ASSIGN_FUNC(func_name) \
CHandler::m_funcmap[STR(func_name)] = (CHandler::pFunc)&func_name;

初始化:

CHTMLRenderer::CHTMLRenderer()
{
ASSIGN_FUNC(CHTMLRenderer::start_document);
ASSIGN_FUNC(CHTMLRenderer::end_document);
ASSIGN_FUNC(CHTMLRenderer::start_paragraph);
ASSIGN_FUNC(CHTMLRenderer::end_paragraph);
ASSIGN_FUNC(CHTMLRenderer::start_heading);
ASSIGN_FUNC(CHTMLRenderer::end_heading);
ASSIGN_FUNC(CHTMLRenderer::start_list);
ASSIGN_FUNC(CHTMLRenderer::end_list);
ASSIGN_FUNC(CHTMLRenderer::start_listitem);
ASSIGN_FUNC(CHTMLRenderer::end_listitem);
ASSIGN_FUNC(CHTMLRenderer::start_title);
ASSIGN_FUNC(CHTMLRenderer::end_title);
ASSIGN_FUNC_SUB(CHTMLRenderer::sub_emphasis);
ASSIGN_FUNC_SUB(CHTMLRenderer::sub_url);
ASSIGN_FUNC_SUB(CHTMLRenderer::sub_mail);
}

调用方法:

void CHandler::callback(const string &str)
{
funcmap_iter iter = m_funcmap.find(str);
if(iter != m_funcmap.end())
(this->*(iter->second))();
else
cout << "invalid function name : " << str << endl;
} void CHandler::start(const string &func_name)
{
callback(string("CHTMLRenderer::start_") + func_name);
} void CHandler::end(const string &func_name)
{
callback(string("CHTMLRenderer::end_") + func_name);
}

3.使用正則表達式

Python标准库提供了re包来进行正則表達式的处理。而c++标准库没有实现regex,boost::regex功能强大,但为了写一个小demo,包括一大堆库太麻烦。

我使用一个轻量级的开源c++实现正则库deelx。官网: http://www.regexlab.com/

整个库就是一个头文件deelx.h,使用时include之就可以。

演示样例:

string filtering(const string& block, const string& pattern,
const string& sub_name, CHandler* handler){
static CRegexpT <char> regexp;
regexp.Compile(pattern.c_str());
MatchResult result = regexp.Match(block.c_str());
if(result.IsMatched()){
char* content = regexp.Replace(block.c_str(),
handler->sub(sub_name).c_str());
string new_block(content);
regexp.ReleaseString(content);
return new_block;
}
return block;
}

deelx源代码与文档可在其官网下载。

结果例如以下图:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2FwdGFpbndvbmc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

《Python基础教程》第20章学习笔记的更多相关文章

  1. 《Java基础教程》第一章学习笔记

    Java 是什么呀! 计算机语言总的来说分成机器语言,汇编语言,高级语言.其中Java一种高级计算机语言,它是一种可以编写跨平台应用软件,完全面向对象的程序设计语言. Java划分为三个技术平台,Ja ...

  2. HTML5与CSS3基础教程第八版学习笔记11~15章

    第十一章,用CSS进行布局 开始布局注意事项 1.内容与显示分离 2.布局方法:固定宽度和响应式布局 固定宽度,整个页面和每一栏都有基于像素的宽度 响应式布局也称为流式页面,使用百分数定义宽度 3.浏 ...

  3. HTML5与CSS3基础教程第八版学习笔记7~10章

    第七章,CSS构造块 CSS里有控制基本格式的属性(font-size,color),有控制布局的属性(position,float),还有决定访问者打印时在哪里换页的打印控制元素.CSS还有很多控制 ...

  4. HTML5与CSS3基础教程第八版学习笔记16-21章

    第十六章,表单 HTML5引入了新的表单元素.输入类型和属性,以及内置的对必填字段.电子邮件地址.URL以及定制模式验证. 元素: <input type="email"&g ...

  5. HTML5与CSS3基础教程第八版学习笔记1~6章

    第一章,网页的构造块 网页主要包括三个部分: 1.文本内容(纯文字) 2.对其他文件的引用:图像,音频,视频,样式表文件,js文件 3.标记:对文本内容进行描述并确保引用正确地工作 注:所有这些成分都 ...

  6. python基础教程第2章——列表与元组笔记

    1.序列是Python中最基本的数据结构.序列中的每个元素被分配一个序列号——元素的位置,也称索引,第1个索引是0,第2为1,以此类推.序列中的最后1个元素为-1,倒数第2个位-2. python中有 ...

  7. python基础教程-第三章-使用字符串

    本章将会介绍如何使用字符串何世华其他的值(如打印特殊格式的字符串),并简单了解下利用字符串的分割.联接.搜索等方法能做些什么 3.1 基本字符串操作 所有标准的序列操作(索引.分片.乘法.判断成员资格 ...

  8. 《Python自然语言处理》第二章 学习笔记

    import nltk from nltk.book import * nltk.corpus.gutenberg.fileids() emma = nltk.corpus.gutenberg.wor ...

  9. python基础教程第4章——字典

    1.映射(mapping):通过名字引用值的数据结构.字典是Python中唯一内建的映射类型,字典中的值并没有特殊的顺序,但是都存储在一个特定的键(key)里.键可以是数字.字符串甚至是元组. 2.字 ...

随机推荐

  1. 左偏树自己的一点理解【hdu1512】【Monkey King】

    [pixiv] https://www.pixiv.net/member_illust.php?mode=medium&illust_id=34693563 向大(hei)佬(e)势力学(di ...

  2. extjs combo中给Store插入一条数据

    { xtype: 'combo', columnWidth: .55, name: 'AQLLevel', store: Ext.create('Scripts.Code.Common.store.I ...

  3. PHP的一些陷阱

    1,空Array为False $a = array() if($a == false) { echo "This will be printed ! ! !" ; } 持续更新中

  4. Facebook KeyHash生成方法

    1. 去https://code.google.com/p/openssl-for-windows/downloads/list下载OpenSSL工具 2.  在C盘根目录下新建一个openssl的文 ...

  5. JAVA常见算法题(一)

    package com.xiaowu.demo; // 有一只兔子,从出生后第3个月起每个月都生一只兔子,小兔子长到第四个月后每个月又生一只兔子,假如兔子都不死,问每个月的兔子总数为多少? /** * ...

  6. kubernetes社区项目生态概览

    原文  http://dockone.io/article/2075 作为容器集群管理技术的最流行的技术,kubernetes,自从2014在github上开源后,已经通过多个项目形成了一个生态,以下 ...

  7. XP右键菜单添加“打开所在文件夹”功能

    把以下文字保存为add.reg文件,双击后确定就可以了. REGEDIT4 [HKEY_CLASSES_ROOT\*\Shell\打开所在文件夹(&O)] [HKEY_CLASSES_ROOT ...

  8. SWIG 多语言接口变换 【转】

    一.             SWIG 是Simple Wrapper and Interface Generator的缩写,是一个帮助使用C或者C++编写的软件创建其他编语言的API的工具.例如,我 ...

  9. ElasticSearch测试数据

    curl命令数据 curl -XPUT http://127.0.0.1:9200/us/user/1 -d "{\"email\":\"john@smith. ...

  10. elasticsearch新加入节点不能识别问题

    向ES集群中新加入节点,配置文件也没有什么问题,但是就是加不进去,这时候就需要检查一下防火墙是否开启.关闭即可