将文件树与文本文件替换同步

filetreesubs的Python项目详细描述


允许从源文件树同步目标文件树 同时允许进行某些替换。

文件树sub使用引擎盖下的doit来保存 跟踪更改,以便仅在必要时更改文件。

有关 filetreesubs。我个人用它来预处理 Nikola,静态博客/站点生成器,到 在所有生成的html页面中插入侧边栏,并在 侧栏和标记概述页。

示例

假设您有以下文件树:

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,并确保替换 发生。

示例:替换链

假设在上面的示例中,您希望在 menu.inc本身。运行上面的示例,将不会进行此替换, 另外,如果将匹配所有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 format中。 默认情况下,配置假定位于filetreesubs-config.yaml 在当前目录中。如果要指定其他配置文件 name,您可以在命令行中指定它:

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'

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

推荐PyPI第三方库


热门话题
javascript节点js require()和Rhino Shell load()之间的区别是什么?   While循环中的java If语句   java GXT如何在XTemplate中显示treeobject?   如何将黑白图像加载到二进制文件中?   java ORMLite不知道如何存储班级航班。扫描仪。modelFX。机场航空公司的AirlineFx。使用另一个类或自定义持久器   java组织。阿帕奇。德比。客户是SqlException JSP和JSTL查询   spring如何将clob数据分配给java变量   java从上的表获取数据(Spring)   javaphp使用Exec执行带有用户定义参数的Jar文件   java jsonschema2pojo:引用相同类型的对象   使用原语从Scala调用Java vararg方法   java ClassNotFoundException:ClientBuilder Jersey 2.25.1 Tomcat 9.0   Android(Java)cr_BindingManager:无法调用determinedVisibility()从未看到pid的连接:   Java使用LocalTime类比较没有日期的时间   Java如何初始化对象的参数?