【问题标题】:Ant replaceregexp to rewrite href links to confluence linksAnt replaceregexp 将 href 链接重写为 confluence 链接
【发布时间】:2014-06-25 20:20:52
【问题描述】:

我需要将 href 链接转换为不同类型的链接(Confluence 有自己的系统),并且我正在接近使用带有 replaceregexp 的 Ant build.xml 文件,但还没有完全实现。

基本上我需要从这样的链接开始:

<a class="xref" href="../Test_Topic_2/Test_Topic_2.txt">Test Topic 2</a>

然后把它们变成这样:

<ac:link><ri:page ri:content-title="Test_Topic_2" /></ac:link>

我在上面的链接上有一个 Ant build.xml 文件,但如果路径以 ../../ 而不是 ../ 开头,它就不起作用

由于获取主题名称的最佳位置是来自“Test_Topic_2.txt”条目,我想知道是否有正则表达式可以从“.txt”逆向工作,告诉它匹配从'.txt' 回到它遇到的第一个斜杠,将其保留在原处,然后替换其余部分。

可能有一些完全不同的方法,如果有人有任何想法,请告诉我。

谢谢,

【问题讨论】:

    标签: regex ant


    【解决方案1】:

    假设输入链接存在于文件input.txt中,与构建文件位于同一路径,内容如下:

    <a class="xref" href="../Test_Topic_1/Test_Topic_1.txt">Test Topic 1</a>
    <a class="xref" href="../Test_Topic_2/Test_Topic_2.txt">Test Topic 2</a>
    <a class="xref" href="../Test_Topic_3/Test_Topic_3.txt">Test Topic 3</a>
    <a class="xref" href="../Test_Topic_4/Test_Topic_4.txt">Test Topic 4</a>
    

    您可以将文件加载到属性中,然后遍历每一行并将其替换为更新的链接,将更新的链接保存在属性中并将其附加到输出文件中,如下所示。

    <loadfile property="file.content" srcFile="./input.txt" />
    
    <for list="${file.content}" param="original.href" delimiter="${line.separator}">
        <sequential>
            <var name="updated.href" unset="true" />  
            <propertyregex input="@{original.href}" property="updated.href" regexp="&lt;a class=&quot;xref&quot; href=&quot;.+/([^/]+)\.txt&quot;&gt;.+&lt;/a&gt;"
                  replace="&lt;ac:link&gt;&lt;ri:page ri:content-title=&quot;\1&quot; /&gt;&lt;/ac:link&gt;" />
            <echo message="${updated.href}${line.separator}" file="output.txt" append="true" />
        </sequential>
    </for>
    

    这种情况下的输出是:

    <ac:link><ri:page ri:content-title="Test_Topic_1" /></ac:link>
    <ac:link><ri:page ri:content-title="Test_Topic_2" /></ac:link>
    <ac:link><ri:page ri:content-title="Test_Topic_3" /></ac:link>
    <ac:link><ri:page ri:content-title="Test_Topic_4" /></ac:link>
    

    【讨论】:

    • 非常感谢,我想我了解您所做的事情,但是您或其他人能否详细说明如何获取您提取的内容,即“Test_Topic_2”到 ac:link 代码中上面给出的?
    • 另外我需要读取目录中的任何 .txt 文件并提取和转换文件中的链接,而不是像上面那样将其硬连接到一个链接作为输入。我意识到那里可能有足够的信息可供某些人推断,但就我而言,如果有人能写出来,将不胜感激。再次感谢。
    【解决方案2】:
    <replaceregexp byline="true">
    
       <regexp pattern="&lt;a class=.*?href=&quot;.*?([^\/]+)\.txt&quot;&gt;.*?&lt;\/a&gt;"/>
       <substitution expression="&lt;ac:link&gt;&lt;ri:page ri:content-title=&quot;\1&quot; \/&gt;&lt;\/ac:link&gt;"/>
    
       <fileset dir="${your.directory.containing.txt.files}">
          <include name="**/*.txt"/>
       </fileset>
    
    </replaceregexp>  
    

    这里,replaceregexp 将处理输入文件的每一行,将其与&lt;regex pattern="..."/&gt; 匹配,并将成功匹配的替换为&lt;substitution expression="..."/&gt;
    这将针对&lt;fileset&gt; 中的每个文件完成。

    所以,例如,如果您有以下目录。结构:

    ../ a / b / 1.txt, 2.txt  
    ../ a / b / c / 3.txt
    

    如果您将${your.directory.containing.txt.files} 设置为../a/b/,那么文件1.txt, 2.txt and 3.txt 将被&lt;replaceregexp&gt; 逐行处理,并且每个匹配的表达式都将被替换

    Demo here

    【讨论】:

      猜你喜欢
      • 2012-02-02
      • 1970-01-01
      • 1970-01-01
      • 2014-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-10
      • 2013-09-09
      相关资源
      最近更新 更多