使用jpg | png进行Ant任务捕获图像扩展

时间:2014-07-04 03:30:50

标签: regex ant ant-contrib

现在重写一遍,我知道更清楚的是:

我有一些Ant propertyregex任务,它们从文件中的图像链接中提取三个属性。然后我想用这些来重写链接。

我到目前为止提取了我需要的属性,然后如果我使用replaceregex任务,我可以使用属性重写链接。但是,即使replaceregex一次只运行一行,完成一行,然后从下一行开始,它将从第一个链接中提取属性,并将它们用作文件中每个链接的替换。 / p>

有没有办法将这两者结合起来,以便整个操作从一个链接开始,提取属性,重写它,然后移动到下一个链接并开始一切?

以下是我所拥有的:

<project name="BuildModule" basedir="." default="extract.stuff">
<taskdef resource="net/sf/antcontrib/antlib.xml" />
<property environment="env" />
<loadfile property="file" srcfile="${basedir}/inputLog.log"/>
<target name="extract.stuff">
    <for param="line" delimiter="${line.separator}" list="${file}">
        <sequential>

            <propertyregex property="imageName" input="@{line}" regexp="(&lt;img class.*?graphics/)(.*?jpg|png)" select="\2" />
            <propertyregex property="width" input="@{line}" regexp="(&lt;img class)(.*?)(width.*?)(alt)" select="\3" />
            <propertyregex property="alt" input="@{line}" regexp="(&lt;img class)(.*?)(width.*?)(alt=)(.*?)(/&gt;)" select="\5" />

        </sequential>
    </for>


     <replaceregexp byline="true">
      <regexp pattern="(&lt;img class)(.*?$)"/>
     <substitution expression="\&lt;ac:image ac:title=${alt} ac:${width}&gt;&lt;ri:attachment ri:filename=&quot;${imageName}&quot;&gt;&lt;ri:page ri:content-title=&quot;acme_shared_graphics&quot; /&gt;&lt;/ri:attachment&gt;&lt;/ac:image&gt;&lt;/p&gt;"/>
     <fileset dir=".">
     <include name="**.log"/>
     </fileset>
   </replaceregexp>

</target>

输入文件中的链接如下:

<p class="p"><img class="image" id="concept_kdv_4zf_k4__image_qt1_pvc_q4" src="../../../shared_assets/acme_shared_graphics/acme_image_test.jpg" width="400" alt="Some alt title text"/></p>

结果如下:

<p class="p"><ac:image ac:title="Some alt title text" ac:width="400" ><ri:attachment ri:filename="acme_image_test.jpg"><ri:page ri:content-title="acme_shared_graphics" /></ri:attachment></ac:image></p>

一旦我开始工作,我希望(.*?jpg|png)选项也能正常运行,根据链接包含的内容提取image_name.jpgimage_name.png,然后替换同样的方式。

更新

这是我现在所拥有的,在这里的帮助之后:

<project name="BuildModule" basedir="." default="extract.stuff">

<taskdef resource="net/sf/antcontrib/antlib.xml" />

<property environment="env" />
<loadfile property="file" srcfile="${basedir}/inputLog.log"/>

<target name="extract.stuff">

    <for param="line" delimiter="${line.separator}" list="${file}">

        <sequential>

        <propertyregex override="true" property="imageName" input="@{line}" regexp="(&lt;img class.*?graphics/)(.*?jpg|png)" select="\2" />
        <propertyregex override="true" property="width" input="@{line}" regexp="(&lt;img class)(.*?)(width.*?)(alt)" select="\3" />
        <propertyregex override="true" property="alt" input="@{line}" regexp="(&lt;img class)(.*?)(width.*?)(alt=)(.*?)(/&gt;)" select="\5" />  

        <replaceregexp byline="true">
        <regexp pattern="(&lt;img class)(.*?$)"/>
        <substitution expression="\&lt;ac:image ac:title=${alt} ac:${width}&gt;&lt;ri:attachment ri:filename=&quot;${imageName}&quot;&gt;&lt;ri:page ri:content-title=&quot;BSW_shared_graphics&quot; /&gt;&lt;/ri:attachment&gt;&lt;/ac:image&gt;&lt;/p&gt;"/>

        <!-- 
        <fileset dir=".">
        <include name="**.log"/>
        </fileset>
        -->

        </replaceregexp>

        </sequential>

    </for>

</target>

评论<fileset></fileset>中的<replaceregexp>是尝试根据<for>循环中指定的输入运行所有内容,因为问题在于<replaceregexp>使用文件集在返回<propertyregex>部分之前逐行执行整个文件。但是现在,如果没有<fileset>,构建只会忽略<replaceregexp>部分。如果我重新放入<fileset>,它就像以前一样,将每个属性更改为文件中遇到的第一个属性。

<propertyregex>添加覆盖修复了有关保留该属性的问题,因此我不需要<unset>

2 个答案:

答案 0 :(得分:0)

在Ant属性中,一旦设置为不可变,因此您需要首先使用antcontrib unset取消设置属性。然后你将获得你的replaceregexp内容for循环:

    <for param="line" delimiter="${line.separator}" list="${file}">
     <sequential>

      <var name="imageName" unset="true"/>
      <var name="width" unset="true"/>
      <var name="alt" unset="true"/>

      <propertyregex property="imageName" input="@{line}" regexp="(&lt;img class.*?graphics/)(.*?jpg|png)" select="\2" />
      <propertyregex property="width" input="@{line}" regexp="(&lt;img class)(.*?)(width.*?)(alt)" select="\3" />
      <propertyregex property="alt" input="@{line}" regexp="(&lt;img class)(.*?)(width.*?)(alt=)(.*?)(/&gt;)" select="\5" />

      <replaceregexp>
       <!-- ... -->
      </replaceregexp>

     </sequential>
    </for>

答案 1 :(得分:0)

<for>循环已在<replaceregexp>之前结束,因此您并未真正为${imageName}, ${width} and ${alt}任务的每一行提取<replaceregexp>的值。因此,由于缺少<propertyregex>,它们可能会保留override="true"经过的 FIRST 行的值。

因此,请在<replaceregexp>中包含<for><sequential></..></..>任务,如下一个答案中所示,或者,而不是在imageName, width and alt表达式中指定<substitution> ...指定组,例如\2, \5, \X..,并将<regex>模式字符串替换为包含整个文件行的模式字符串。

如:

<regexp pattern="&lt;img class.*?/(.*?graphics)/(.*?)&quot; width=&quot;(.*?)&quot; -alt=&quot;(.*?)&quot;/&gt;"/>
<substitution expression="\&lt;ac:image ac:title=&quot;\4&quot; ac:&quot;\3&quot;&gt;&lt;ri:attachment ri:filename=&quot;\2&quot;&gt;&lt;ri:page ri:content-title=&quot;\1&quot; /&gt;&lt;/ri:attachment&gt;&lt;/ac:image&gt;&lt;/p&gt;"/>

编辑:

propertyregex已允许属性 可变性 。因此,imageName, alt and width很好,可以更改( 只需添加override="true"作为属性 - <propertyregex ... override="true".../>,您就不需要任何unset="true" )。

first line values for every lineimageName, alt and width <propertyregex> override="true" <for> <replaceregexp>错过了<sequential>,因此第一次设置这些属性时,它们仍然< override的后续循环中强>未变 现在,

- 在<propertyregex .. override="true".. />中加入<replaceregexp>   - 将<fileset>添加到所有三个属性:<sequential>

(还有,您当前的fileset basedir针对{{1>}中的 所有 文件运行。如果您将其包含在<include name= regex>中确保避免grouping中的无关文件被<for> & <propertyregexp>提取

它应该有效。

OR,尝试我提到的target方法。我会选择这个。 在这里,我想,你 根本不需要任何 <replaceregexp>。只需在整个<fileset>上执行<regex pattern <substitution expression任务,就像您一样,但更改<propertyregex>和{ {1}}以容纳 分组

事实上,如果仔细观察,relevant groupings所做的只是从输入行中选择 <replaceregexp> ,并且您的实现 - <replaceregexp>取代文件/行中的那些值。 使用<replaceregexp>任务中的分组将这两者结合起来。

有关{{1}}的更多信息,请参阅HERE