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. pr_debug、dev_dbg等动态调试二

    内核版本:Linux-3.14 作者:彭东林 邮箱:pengdonglin137@163.com 下面我们简要分析 1: echo -n "file demo.c +p" > ...

  2. Glide使用详解(一)

    一. 下载 在build.gradle中添加依赖: compile 'com.github.bumptech.glide:glide:3.7.0' 需要support-v4库的支持,如果你的项目没有s ...

  3. PSR规范,链接转载

    https://www.kancloud.cn/thinkphp/php-fig-psr

  4. 详细理解javascript中的强制类型转换

    将值从一种类型转换为另一种类型通常称为类型转换,这是显式的情况:隐式的情况称为强制类型转换,JavaScript 中的强制类型转换总是返回标量基本类型值,如字符串.数字和布尔值. 如何理解: 类型转换 ...

  5. Linux expect 用法

    expect是建立在tcl基础上的一个工具,它用来让一些需要交互的任务自动化地完成. 因为expect是基于tcl的,所以需要你的系统中安装有tcl 检查是否安装tcl,expect [root@ma ...

  6. 最长公共字串算法, 文本比较算法, longest common subsequence(LCS) algorithm

    ''' merge two configure files, basic file is aFile insert the added content of bFile compare to aFil ...

  7. 2017.4.10 spring-ldap官方文档学习

    官网:http://www.springframework.org/ldap 官方文档及例子(重要):http://docs.spring.io/spring-ldap/docs/2.1.0.RELE ...

  8. spring 配置多数据源 (案例)

    *.properties配置: <!--数据连接配置一--> jdbc.type=mysqljdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:m ...

  9. How to Configure an SSIS Package to Access a Web Service using WCF

    This information is from:http://blogs.msdn.com/b/dbrowne/archive/2010/07/08/how-to-configure-an-ssis ...

  10. GROUP BY和HAVING 以及mysql中常用的日期函数

    一.mysql中的GROUP BY和HAVINGGROUP BY常见的是和聚合函数(SUM,MIN,MAX,COUNT)搭配使用. 比如:SELECT category,SUM(money) AS ` ...