这个包提供了structuredtext包,正如zope 2所说。除非需要与zope2api通信,否则最好使用较新的zope.structuredtext模块。

StructuredText的Python项目详细描述


使用结构化文本

WARNING! The ‘StructuredText’ package has been deprecated and will be removed in Zope 2.12. Use ‘zope.structuredtext’ instead.

The goal of StructuredText is to make it possible to express structured text using a relatively simple plain text format. Simple structures, like bullets or headings are indicated through conventions that are natural, for some definition of “natural”. Hierarchical structures are indicated through indentation. The use of indentation to express hierarchical structure is inspired by the Python programming language.

Use of StructuredText consists of one to three logical steps. In the first step, a text string is converted to a network of objects using the ‘StructuredText.Basic’ facility, as in the following example:

raw=open("mydocument.txt").read()
import StructuredText
st=StructuredText.Basic(raw)

The output of ‘StructuredText.Basic’ is simply a StructuredTextDocument object containing StructuredTextParagraph objects arranged in a hierarchy. Paragraphs are delimited by strings of two or more whitespace characters beginning and ending with newline characters. Hierarchy is indicated by indentation. The indentation of a paragraph is the minimum number of leading spaces in a line containing non-white-space characters after converting tab characters to spaces (assuming a tab stop every eight characters).

StructuredTextNode objects support the read-only subset of the Document Object Model (DOM) API. It should be possible to process ‘StructuredTextNode’ hierarchies using XML tools such as XSLT.

The second step in using StructuredText is to apply additional structuring rules based on text content. A variety of differentText rules can be used. Typically, these are used to implement a structured text language for producing documents, but any sort of structured text language could be implemented in the second step. For example, it is possible to use StructuredText to implement structured text formats for representing structured data. The second step, which could consist of multiple processing steps, is performed by processing, or “coloring”, the hierarchy of generic StructuredTextParagraph objects into a network of more specialized objects. Typically, the objects produced should also implement the DOM API to allow processing with XML tools.

A document processor is provided to convert a StructuredTextDocument object containing only StructuredStructuredTextParagraph objects into a StructuredTextDocument object containing a richer collection of objects such as bullets, headings, emphasis, and so on using hints in the text. Hints are selected based on conventions of the sort typically seen in electronic mail or news-group postings. It should be noted, however, that these conventions are somewhat culturally dependent, fortunately, the document processor is easily customized to implement alternative rules. Here’s an example of using the DOC processor to convert the output of the previous example:

doc=StructuredText.Document(st)

The final step is to process the colored networks produced from the second step to produce additional outputs. The final step could be performed by Python programs, or by XML tools. A Python outputter is provided for the document processor output that produces Hypertext Markup Language (HTML) text:

html=StructuredText.HTML(doc)

自定义文档处理程序

The document processor is driven by two tables. The first table, named ‘paragraph_types’, is a sequence of callable objects or method names for coloring paragraphs. If a table entry is a string, then it is the name of a method of the document processor to be used. For each input paragraph, the objects in the table are called until one returns a value (not ‘None’). The value returned replaces the original input paragraph in the output. If none of the objects in the paragraph types table return a value, then a copy of the original paragraph is used. The new object returned by calling a paragraph type should implement the ReadOnlyDOM, StructuredTextColorizable, and StructuredTextSubparagraphContainer interfaces. See the ‘Document.py’ source file for examples.

A paragraph type may return a list or tuple of replacement paragraphs, this allowing a paragraph to be split into multiple paragraphs.

The second table, ‘text_types’, is a sequence of callable objects or method names for coloring text. The callable objects in this table are used in sequence to transform the input text into new text or objects. The callable objects are passed a string and return nothing (‘None’) or a three-element tuple consisting of:

  • a replacement object,
  • a starting position, and
  • an ending position

起始位置的文本(逻辑上)替换为 替换对象。替换对象通常是一个对象 实现了readonlydom,并且 StructuredTextColorizable接口。替换对象可以 也可以是字符串或字符串或对象列表。替换是 自始至终完成,替换结束后文本 位置将传递给字符类型对象进行处理。

示例:添加wiki链接

We want to add support for Wiki links. A Wiki link is a string of text containing mixed-case letters, such that at least two of the letters are upper case and such that the first letter is upper case.

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
尝试通过java驱动程序连接时,mongodb服务器上的SSLhandshake失败   使用PlayFramework的Azure网站中的java Logback   java在另一个ArrayList中使用ArrayList处理复杂的JSON响应   java无法在另一台机器上运行eclipse tomcat中的war文件   java GZIPOutputStream有什么替代方案吗?   java Nashorn调试在Nashorn中运行的javascript   java文本短信未发送,即使toast显示已发送   java Hibernatesearch 5.0 spatial不确定是否在散列中存储lat/lon   java我想创建一个带有文本视图的计数器   java安卓:如何正确地同步资源   java使用mockito。当不知道方法调用的参数时   firebase Java使用HTTP v1发送错误字符的中文通知   java Hibernate无法映射到表?   java使用对象映射器解析复杂JSON   java Selenium Grid 2并行测试用例执行   java所有项目在列表视图中重复