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. linux下安装 tomcat

    1.首先配置jdk,上篇文章中有具体的介绍. 2.官网下载tomcat:https://tomcat.apache.org/download-80.cgi (下载 tar.gz 的版本 ) 3.上传压 ...

  2. C++ development cross platforms

    1. target platforms: linux suse, windows server, both use vmware virtual machine on lab server. 2. c ...

  3. JAVA常见算法题(十五)

    package com.xiaowu.demo; /** * * 输入三个整数x,y,z,请把这三个数由小到大输出. * * @author WQ * */ public class Demo15 { ...

  4. jq和js中click 事件的几种方式总结和click事件的累加问题解决办法

     1:常见的三种绑定click事件: 第一种:$("#click").click(function(){ alert("Hello World  click") ...

  5. pycharm批量清楚pyc、$py.class文件

    By default, the .pyc and $py.class files are ignored, and thus are not visible in the Project tool w ...

  6. python+ubuntu+selenium安装chrome和chromedriver

    请确保selenium已经安装成功,没安装的可以pip install selenium 安装chrome 在终端输入 下载安装包 wget https://dl.google.com/linux/d ...

  7. log4j教程 8、日志格式化

    Apache log4j 提供了各种布局对象,每一个对象都可以根据各种布局格式记录数据.另外,也可以创建一个布局对象格式化测井数据中的特定应用的方法. 所有的布局对象 - Appender对象收到 L ...

  8. UI自动化测试篇 :ReportNG替代TestNG自带html版测试报告初探

    转载http://www.cnblogs.com/chengtch/p/6071322.html “1.1.4版本的ReportNG是最后一个版本,之后不再做维护.作为一个简单的测试报告插件,它是创造 ...

  9. Sublime Text 3 文档

    中文版:http://feliving.github.io/Sublime-Text-3-Documentation/ 英文版:http://www.sublimetext.com/docs/3/

  10. 渗透测试中的文件传输通道1- cmd下下载文件

    Set xPost = createObject("Microsoft.XMLHTTP")xPost.Open "GET","http://www.x ...