ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • jsx와 xslt
    TeX과 친구들 2024. 4. 18. 10:15

    인디자인은 xml 내보내기/가져오기 기능을 제공한다. 이런저런 이유로 그 기능을 활용하는 jsx 스크립트를 오래 전에 퇴사한 친구가 만들었다. 그 스크립트를 고쳐달라는 요청을 받았다. xml 파일로 저장할 때 이미지 파일들의 경로가 절대 경로로 되어 있는데, 그 때문에 다른 사람의 컴퓨터에서 xml 파일을 불러들일 때 이미지들을 다시 링크해야 하는 피할 수 없는 번거로움이 발생한다. 그러니까 xml 파일을 저장할 때 이미지 경로를 상대 경로로 바꾸어 달라는 것이다.

    인디자인은 이 기능에서 xslt를 지원한다. tokenize()를 비롯한 여러 시도 뒤에 인디자인이 XSLT 2.0을 지원하지 않는다는 것을 알아내었다. tokenize()를 대신할, 정확히 말해 슬래시를 기준으로 재귀적으로 문자열을 잘라내는 템플릿을 만들어야 한다. xslt 문법에 대해 아는 바가 별로 없는지라 챗지피티에게 방법을 물었다. 그것이 제시한 코드가 유효하지 않았지만 도움이 되었다. 다음과 같이 고쳐서 뜻한 바를 이루었다.

    <xsl:template match="Img-Left/@href | Img-Center/@href | Img-Right/@href | C_Image/@href">
            <xsl:attribute name="href">
                <xsl:choose>
                    <xsl:when test="contains(., '_Links/')">
                        <xsl:call-template name="get-link-folder">
                            <xsl:with-param name="head" select="substring-before(., '_Links/')"/>
                        </xsl:call-template>                                                    
                        <xsl:value-of select="'_Links/'"/>
                        <xsl:value-of select="substring-after(., '_Links/')"/>
                    </xsl:when>
                </xsl:choose>
            </xsl:attribute>
        </xsl:template>
    
        <xsl:template name="get-link-folder">
            <xsl:param name="head"/>
            <xsl:param name="delimiter" select="'/'"/>
            <xsl:choose>
                <xsl:when test="contains($head, $delimiter)">
                    <xsl:call-template name="get-link-folder">
                        <xsl:with-param name="head" select="substring-after($head, $delimiter)"/>
                    </xsl:call-template>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$head"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:template>

     

    댓글

Designed by Tistory.