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. viewport 测试以及总结

    这里的总结的主要思想是ppk的文章(文末附有链接),加入了自己的总结,下面的测试用的是iphone5s,android是安卓5.5吋的手机,只是为了直观感受和方便解释拿了空出来的测试机给出的数据.详细 ...

  2. python全栈开发- day14列表推导式、生成器表达式、模块基础

    一.列表推导式 #1.示例 数据量小 egg_list=[] for i in range(10): egg_list.append('鸡蛋%s' %i) egg_list=['鸡蛋%s' %i fo ...

  3. Jenkins忘记密码的修复方法(Windows/Linux)

    在jenkins的安装目录下,找到config.xml配置文件,删除以下节点: <useSecurity>true</useSecurity> <authorizatio ...

  4. [置顶] kubernetes资源类型--DaemonSet

    概念 DaemonSet能够让所有(或者特定)的节点运行同一个pod. 当节点加入到K8S集群中,pod会被(DaemonSet)调度到该节点上运行,当节点从K8S集群中被移除,被DaemonSet调 ...

  5. Python 把u'\xca\xd3\xc6\xb5\xd7\xa5\xc8\xa1' 输出正常中文

    今天碰见从数据库读取出来数据是u'\xca\xd3\xc6\xb5\xd7\xa5\xc8\xa1',输出显示乱码,经常查询处理如下: 两种方式: 1. s = u'\xca\xd3\xc6\xb5\ ...

  6. Java源码阅读ArrayList

    1简介 public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAc ...

  7. android 小技巧

    1. 模拟器横坚屏切换 ctrl + F11, ctrl + F12

  8. hdu 1030 Delta-wave(数学题+找规律)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1030 Delta-wave Time Limit: 2000/1000 MS (Java/Others ...

  9. Android学习(十) SQLite 基于SQL语句的操作方式

    main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns ...

  10. Android高级控件(三)——&#160;使用Google ZXing实现二维码的扫描和生成相关功能体系

    Android高级控件(三)-- 使用Google ZXing实现二维码的扫描和生成相关功能体系 摘要 如今的二维码可谓是烂大街了.到处都是二维码.什么都是二维码,扫一扫似乎已经流行到习以为常了,今天 ...