同步文件树与文本文件替换
项目描述
文件树替换
允许从源文件树同步目标文件树,同时允许进行某些替换。
File Tree Subs在底层使用doit来跟踪更改,以确保只有在必要时才更改文件。
请参阅以下三个示例,了解filetreesubs的典型用例。我将其用于预处理Nikola(一个静态博客/网站生成器)的输出,以便在所有生成的HTML页面中插入侧边栏,并在侧边栏和标签概览页中插入标签云。
安装,请使用pip install filetreesubs
。
示例
假设你有以下文件树
input/
index.html
team.html
products.html
menu.inc
testimonials.inc
在.html
文件中,你将占位符字符串INSERT_MENU_HERE
放置在input/menu.inc
内容应插入的位置,将INSERT_TESTIMONIALS
放置在input/testimonials.inc
内容应插入的位置。你还想将COPYRIGHT_YEAR
替换为2017。结果应该是一个没有.inc
文件的树
output/
index.html
team.html
products.html
占位符字符串被替换。要使用filetreesubs完成此操作,请创建一个配置文件filetreesubs-config.yaml
# Source directory
source: input
# Destination directory
destination: output
substitutes:
# The following is a regular expression to match the filenames:
'.*\.html':
# The strings to replace
'INSERT_MENU_HERE':
# With what to replace them
file: menu.inc
'INSERT_TESTIMONIALS':
file: testimonials.inc
'COPYRIGHT_YEAR':
text: '2017'
然后运行filetreesubs将output/
同步,使其包含来自input/
的文件,除了menu.inc
,并确保替换发生。
示例:Nikola中的侧边栏
您可以在侧边栏插件中找到用于Nikola的示例网站,该插件位于felixfontein/filetreesubs-nikola-demo的GitHub仓库。
一个更复杂但不太明确的示例可以在我的博客中找到,该博客还包括一个标签云(由static_tag_cloud插件渲染的侧边栏)。
示例:替换链
假设在上面的例子中,您想在menu.inc
本身也使用INSERT_TESTIMONIALS
。运行上面的例子,即使您将匹配所有HTML文件的正则表达式扩展到.*
以匹配所有文件,此替换也不会进行。
要将替换应用于包含的文件,您需要使用替换链。将以下内容追加到上述配置中
substitute_chains:
- template: menu.inc
substitutes:
'INSERT_TESTIMONIALS':
file: testimonials.inc
这将使INSERT_TESTIMONIALS
的替换也应用于menu.inc
。
示例:创建索引文件
假设您的文件夹结构如下
input/
index.html
images/
logo.jpeg
2017/
happynewyear-2017.jpeg
您想将输出上传到Web服务器,使其在http://example.com
下可用,但如果有人访问http://example.com/images/
或http://example.com/images/2017/
,您不希望人们看到文件列表或错误页面,而是显示一条漂亮的消息,让他们查看主页。您可以使用filetreesubs
来实现这一点。将以下内容添加到配置中
create_index_filename: index.html
create_index_content: |
<!DOCTYPE html>
<html>
<head>
<title>There's nothing to see here.</title>
<meta http-equiv="refresh" content="10; url=..">
</head>
<body>
There's nothing to see here. Go <a href="..">here</a> instead.
You will be automatically redirected there in 10 seconds.
</body>
</html>
然后在每个不包含index.html
文件的文件夹中,将创建一个包含指定内容的index.html
文件。
配置文件格式
配置文件使用YAML格式。默认情况下,配置假定位于当前目录的filetreesubs-config.yaml
中。如果您想指定不同的配置文件名,您可以在命令行中简单地指定它
filetreesubs my-config-file.yaml
以下带注释的YAML文件显示了所有可用的选项
# The source directory. Specify a path here.
source: input
# The destination directory. Specify a path here.
destination: output
# The substitutions to make
substitutes:
# For every substitution, you need to specify a regex pattern
# matching the file name. Use '.*' to match everything, and
# '.*\.html' to match all files ending with '.html'.
'.*':
# Now you can specify a number of strings which shall be replaced
'STRING TO REPLACE':
# In this case, we want to replace the string by the contents
# of the file menu.inc. Note that menu.inc won't be copied
# to the destination directory anymore.
file: menu.inc
'ANOTHER_REPLACEMENT_STRING':
# In this case, we want to replace the string by another string
# we explicitly specify here.
text: '(replacement text)'
# Now we can specify more filename matching patterns ...
'.*\.html':
# ... and more replacements
'YET_ANOTHER_STRING':
text: '(some more)'
# To do substitutions in files like menu.inc, we need substitution
# chains.
substitute_chains:
# Each substitution chain consists of the name of the file to
# substitute in, like menu.inc:
- template: menu.inc
# As well as a list of substitutions, using the same syntax as above:
substitutes:
# The string to replace:
'INCLUDE_INCLUDE':
# What to replace it with
file: include.inc
'INCLUDE_STRING':
text: '...'
# You can have as many substitution chains as you want
- template: include.inc
substitutes:
'ONE_MORE':
text: '(...)'
# To create index files (when not already existing), you must
# specify the name of these files:
create_index_filename: index.html
# This allows to specify the content of index files.
create_index_content: |
<!DOCTYPE html>
<html lang="en">
<head>
<title>there's nothing to see here.</title>
<meta name="robots" content="noindex">
<meta http-equiv="refresh" content="0; url=..">
</head>
<body style="background-color:black; color:white;">
<div style="position:absolute; top:0; left:0; right:0; bottom:0;">
<div style="width:100%; height:100%; display:table;">
there's nothing to see here. go <a href=".." style="color:#AAA;">here</a> instead.
</div>
</div>
</div>
</body>
</html>
# By default, filetreesubs assumes that all text files it processes
# are UTF-8 encoded. If that's not the case, you can change another
# encoding here.
encoding: utf-8
# In case you need to do so, you can insert configurations for doit
# directly here. See `here <http://pydoit.org/configuration.html#configuration-at-dodo-py>`__
# for possible configurations.
doit_config:
# The following option sets the filename for the dependency database.
# If you want to execute different filetreesubs commands concurrently
# from a folder, you need to specify different dependency database
# names per project config.
dep_file: '.doit-myproject.db'
项目详情
下载文件
下载您平台上的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。
源分布
构建分布
filetreesubs-1.2.0.tar.gz的哈希
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 897a2ea2b46d1fa0524caf1080da175acf4688fda5949d28f91a6e9e732f7985 |
|
MD5 | 03f3375660064dfc3fa30381ad4dc024 |
|
BLAKE2b-256 | a3c38189944954e39bc62095204c466ad94ad964deb589efa8999366888afe32 |
filetreesubs-1.2.0-py3-none-any.whl的哈希
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 4e1649d6ef4a767e103bfbce97ce4f136b02a13c0f176502ceb7c852f2fa59b8 |
|
MD5 | fada7eed8e6f48c56a87f07a723a9535 |
|
BLAKE2b-256 | c69f83a9ab50c6f9e0ff36e3f47c5f004473bdce2bcc5d58225f1c0f68e2931b |