This is the mail archive of the docbook-apps@lists.oasis-open.org mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [docbook-apps] [Website] Open links in new window?


I've customized Website 2.5.0 to generate alternate frame link targets (for example, href target="_blank") in the toc and in webpages.
They enable me to specify individual link targets in layout.xml and in webpages, superseding the global ulink.target param.

I don't know how bulletproof my code is, but it works for me. So below are the changes from my customization layer, in case anyone else can use them.
Note the customization of autolayout, and the one template from Docbook XSL 1.64.1.

<!-- =============================================================
     from autolayout.xsl
     ============================================================ -->

<!-- Enable layout to specify the target of an href, for example
     '_blank' (a new window) instead of the default '_top' behavior. -->

<xsl:template name="tocentry.href">
  <xsl:if test="not(@href)">
    <xsl:message terminate="yes">
      <xsl:text>All toc entries must have an href attribute.</xsl:text>
    </xsl:message>
  </xsl:if>

  <xsl:if test="not(@id)">
    <xsl:message terminate="yes">
      <xsl:text>All href toc entries must have an id attribute.</xsl:text>
    </xsl:message>
  </xsl:if>

  <xsl:message>
    <xsl:text>off site: </xsl:text>
    <xsl:value-of select="@href"/>
  </xsl:message>

  <xsl:attribute name="id">
    <xsl:value-of select="@id"/>
  </xsl:attribute>
  <xsl:attribute name="href">
    <xsl:value-of select="@href"/>
  </xsl:attribute>

<!-- DB change here -->
  <xsl:if test="@framelink != ''">
  <xsl:attribute name="framelink">
    <xsl:value-of select="@framelink"/>
  </xsl:attribute>
  </xsl:if>

  <xsl:if test="@tocskip = '1'">
    <xsl:attribute name="tocskip">
      <xsl:value-of select="@tocskip"/>
    </xsl:attribute>
  </xsl:if>

  <xsl:if test="not(title)">
    <xsl:message terminate="yes">
      <xsl:text>Off-site links must provide a title.</xsl:text>
    </xsl:message>
  </xsl:if>

  <xsl:text>&#10;</xsl:text>
  <xsl:apply-templates select="title|titleabbrev|summary" mode="copy"/>
  <xsl:text>&#10;</xsl:text>
  <xsl:apply-templates select="tocentry"/>
</xsl:template>


<!-- ============================================================= from docbook/html/xref.xsl ============================================================ --> <!-- Customization based on Micheal Wiedmann's suggestion in docbook-apps (I updated for newer distro). Generate href framelink target attribute, locally overriding the global ulink.target param. -->

  <!-- Change: define target attribute using either ulink role or
       global ulink.target param -->
  <xsl:template match="ulink" name="ulink">
    <xsl:param name="linktarget">
        <xsl:choose>
          <xsl:when test="@role !=''">
            <xsl:value-of select="@role"/>
          </xsl:when>

          <xsl:when test="$ulink.target != ''">
              <xsl:value-of select="$ulink.target"/>
          </xsl:when>

          <xsl:otherwise/>
        </xsl:choose>
    </xsl:param>

    <xsl:variable name="link">
      <a>
        <xsl:if test="@id">
          <xsl:attribute name="name">
            <xsl:value-of select="@id"/>
          </xsl:attribute>
        </xsl:if>
        <xsl:attribute name="href"><xsl:value-of select="@url"/></xsl:attribute>
        <xsl:if test="$linktarget !=''">
          <xsl:attribute name="target">
            <xsl:value-of select="$linktarget"/>
          </xsl:attribute>
        </xsl:if>

        <xsl:choose>
          <xsl:when test="count(child::node())=0">
            <xsl:value-of select="@url"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:apply-templates/>
          </xsl:otherwise>
        </xsl:choose>
      </a>
    </xsl:variable>

    <xsl:choose>
      <xsl:when test="function-available('suwl:unwrapLinks')">
        <xsl:copy-of select="suwl:unwrapLinks($link)"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:copy-of select="$link"/>
      </xsl:otherwise>
    </xsl:choose>

</xsl:template>

<!-- =============================================================
     from website-common.xsl
     ============================================================ -->

<!-- Change: generate target attribute if framelink param is passed in -->
<xsl:template name="link.to.page">
  <xsl:param name="href" select="''"/>
  <xsl:param name="frompage"/>
  <xsl:param name="page" select="ancestor-or-self::tocentry"/>
  <xsl:param name="relpath">
    <xsl:choose>
      <xsl:when test="$frompage">
        <xsl:call-template name="toc-rel-path">
          <xsl:with-param name="pageid" select="$frompage/@id"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name="toc-rel-path">
          <xsl:with-param name="pageid" select="$page/@id"/>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:param>
  <xsl:param name="linktext" select="'???'"/>
  <xsl:param name="framelink"  select="''"/>
  <a>
    <xsl:attribute name="href">
      <xsl:call-template name="page.uri">
        <xsl:with-param name="href" select="$href"/>
        <xsl:with-param name="page" select="$page"/>
        <xsl:with-param name="relpath" select="$relpath"/>
      </xsl:call-template>
    </xsl:attribute>
    <xsl:if test="$framelink != ''">
      <xsl:attribute name="target">
        <xsl:value-of select="'$framelink'"/>
      </xsl:attribute>
    </xsl:if>
    <xsl:if test="summary">
      <xsl:attribute name="title">
        <xsl:value-of select="summary"/>
      </xsl:attribute>
    </xsl:if>
    <xsl:copy-of select="$linktext"/>
  </a>
</xsl:template>


<!-- ============================================================= from toc-tabular.xsl ============================================================ --> <!-- add parameter for href framelink attribute when calling link.to.page -->

<xsl:template match="tocentry">
  <xsl:param name="pageid" select="@id"/>
  <xsl:param name="toclevel" select="count(ancestor::*)"/>
  <xsl:param name="relpath" select="''"/>
  <xsl:param name="revisionflag" select="@revisionflag"/>

  <xsl:variable name="page" select="."/>
  <xsl:variable name="target"
                select="($page/descendant-or-self::tocentry[@tocskip = '0']
                       |$page/following::tocentry[@tocskip='0'])[1]"/>

<xsl:variable name="depth" select="count(ancestor::*)-1"/>

  <xsl:variable name="isdescendant">
    <xsl:choose>
      <xsl:when test="ancestor::*[@id=$pageid]">1</xsl:when>
      <xsl:otherwise>0</xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:variable name="hasdescendant">
    <xsl:choose>
      <xsl:when test="descendant::tocentry != ''">1</xsl:when>
      <xsl:otherwise>0</xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:variable name="isancestor">
    <xsl:choose>
      <xsl:when test="descendant::*[@id=$pageid]">1</xsl:when>
      <xsl:otherwise>0</xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:variable name="use.toc.expand.depth">
    <xsl:variable name="config-param" select="ancestor::autolayout/config[@param='toc.expand.depth']/@value"/>
    <xsl:choose>
      <!-- toc.expand.depth attribute is not in DTD -->
      <xsl:when test="ancestor::toc/@toc.expand.depth">
        <xsl:value-of select="ancestor::toc/@toc.expand.depth"/>
      </xsl:when>
      <xsl:when test="floor($config-param) > 0">
        <xsl:value-of select="$config-param"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$toc.expand.depth"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:variable name="is.open">
    <xsl:choose>
      <xsl:when test="$pageid = @id
                      or $isancestor='1'
                      or $depth &lt; $use.toc.expand.depth">1</xsl:when>
      <xsl:otherwise>0</xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:variable name="preceding-icon">
    <xsl:value-of select="$relpath"/>
    <xsl:value-of select="$nav.icon.path"/>
    <xsl:value-of select="$nav.icon.style"/>
    <xsl:choose>
      <xsl:when test="$pageid=@id">
        <xsl:choose>
          <xsl:when test="$hasdescendant != 0">
            <xsl:text>/current/open</xsl:text>
          </xsl:when>
          <xsl:otherwise>
            <xsl:text>/current/leaf</xsl:text>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <xsl:otherwise>
        <xsl:choose>
          <xsl:when test="$hasdescendant = 0">
            <xsl:text>/other/leaf</xsl:text>
          </xsl:when>
          <xsl:when test="$is.open != 0">
            <xsl:text>/other/open</xsl:text>
          </xsl:when>
          <xsl:otherwise>
            <xsl:text>/other/closed</xsl:text>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:otherwise>
    </xsl:choose>
    <xsl:value-of select="$nav.icon.extension"/>
  </xsl:variable>

  <xsl:variable name="preceding-text">
    <xsl:choose>
      <xsl:when test="$pageid=@id">
        <xsl:choose>
          <xsl:when test="$hasdescendant != 0">
            <xsl:value-of select="$nav.text.current.open"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="$nav.text.current.page"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <xsl:otherwise>
        <xsl:choose>
          <xsl:when test="$isancestor != 0">
            <xsl:value-of select="$nav.text.other.open"/>
          </xsl:when>
          <xsl:when test="$hasdescendant != 0">
            <xsl:value-of select="$nav.text.other.closed"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="$nav.text.other.page"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:variable name="following-icon">
    <xsl:value-of select="$relpath"/>
    <xsl:value-of select="$nav.icon.path"/>
    <xsl:value-of select="$nav.icon.style"/>
    <xsl:text>/current/pointer</xsl:text>
    <xsl:value-of select="$nav.icon.extension"/>
  </xsl:variable>

  <xsl:variable name="following-text">
    <xsl:value-of select="$nav.text.pointer"/>
  </xsl:variable>

  <xsl:variable name="revisionflag-icon">
    <xsl:value-of select="$relpath"/>
    <xsl:value-of select="$nav.icon.path"/>
    <xsl:value-of select="$nav.icon.style"/>
    <xsl:text>/</xsl:text>
    <xsl:value-of select="$revisionflag"/>
    <xsl:value-of select="$nav.icon.extension"/>
  </xsl:variable>

  <xsl:variable name="revisionflag-text">
    <xsl:choose>
      <xsl:when test="$revisionflag = 'changed'">
        <xsl:value-of select="$nav.text.revisionflag.changed"/>
      </xsl:when>
      <xsl:when test="$revisionflag = 'added'">
        <xsl:value-of select="$nav.text.revisionflag.added"/>
      </xsl:when>
      <xsl:when test="$revisionflag = 'deleted'">
        <xsl:value-of select="$nav.text.revisionflag.deleted"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$nav.text.revisionflag.off"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <span>
    <xsl:if test="$toclevel = 2">
      <xsl:attribute name="class">
        <xsl:text>toplevel</xsl:text>
      </xsl:attribute>
    </xsl:if>

    <xsl:if test="$toclevel &gt; 2">
      <xsl:attribute name="class">
        <xsl:text>shrink</xsl:text>
        <xsl:value-of select="$toclevel - 2"/>
      </xsl:attribute>
    </xsl:if>

    <xsl:call-template name="insert.spacers">
      <xsl:with-param name="count" select="$toclevel - 1"/>
      <xsl:with-param name="relpath" select="$relpath"/>
    </xsl:call-template>

    <xsl:choose>
      <xsl:when test="$nav.graphics != 0">
        <xsl:call-template name="link.to.page">
          <xsl:with-param name="href" select="@href"/>
          <xsl:with-param name="page" select="$target"/>
          <xsl:with-param name="relpath" select="$relpath"/>
          <xsl:with-param name="linktext">
            <img src="{$preceding-icon}" alt="{$preceding-text}" border="0"/>
          </xsl:with-param>
          <xsl:with-param name="framelink" select="@framelink"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$preceding-text"/>
      </xsl:otherwise>
    </xsl:choose>

    <xsl:choose>
      <xsl:when test="$pageid = @id">
        <span class="curpage">
          <xsl:choose>
            <xsl:when test="titleabbrev">
              <xsl:apply-templates select="titleabbrev"/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:apply-templates select="title"/>
            </xsl:otherwise>
          </xsl:choose>

          <xsl:if test="$nav.revisionflag != '0' and $revisionflag">
            <xsl:value-of select="$nav.text.spacer"/>
            <xsl:choose>
              <xsl:when test="$nav.graphics = '1'">
                <img src="{$revisionflag-icon}" alt="{$revisionflag-text}" align="bottom"/>
              </xsl:when>
              <xsl:otherwise>
                <xsl:text>(</xsl:text>
                <xsl:value-of select="$revisionflag-text"/>
                <xsl:text>)</xsl:text>
              </xsl:otherwise>
            </xsl:choose>
	  </xsl:if>

          <xsl:if test="$nav.pointer != '0'">
            <xsl:value-of select="$nav.text.spacer"/>
            <xsl:choose>
              <xsl:when test="$nav.graphics = '1'">
                <img src="{$following-icon}" alt="{$following-text}"/>
              </xsl:when>
              <xsl:otherwise>
                <xsl:value-of select="$following-text"/>
              </xsl:otherwise>
            </xsl:choose>
	  </xsl:if>
        </span>
        <br/>
      </xsl:when>
      <xsl:otherwise>
        <span>
          <xsl:choose>
            <xsl:when test="$isdescendant='0'">
              <xsl:choose>
                <xsl:when test="$isancestor='1'">
                  <xsl:attribute name="class">ancestor</xsl:attribute>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:attribute name="class">otherpage</xsl:attribute>
                </xsl:otherwise>
              </xsl:choose>
            </xsl:when>
            <xsl:otherwise>
              <!-- IS a descendant of curpage -->
              <xsl:attribute name="class">descendant</xsl:attribute>
            </xsl:otherwise>
          </xsl:choose>

          <xsl:call-template name="link.to.page">
            <xsl:with-param name="href" select="@href"/>
            <xsl:with-param name="page" select="$target"/>
            <xsl:with-param name="relpath" select="$relpath"/>
            <xsl:with-param name="linktext">
              <xsl:choose>
                <xsl:when test="titleabbrev">
                  <xsl:apply-templates select="titleabbrev"/>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:apply-templates select="title"/>
                </xsl:otherwise>
              </xsl:choose>
            </xsl:with-param>
          <xsl:with-param name="framelink" select="@framelink"/>
          </xsl:call-template>

          <xsl:if test="$nav.revisionflag != '0' and $revisionflag">
            <xsl:value-of select="$nav.text.spacer"/>
            <xsl:choose>
              <xsl:when test="$nav.graphics = '1'">
                <img src="{$revisionflag-icon}" alt="{$revisionflag-text}"/>
              </xsl:when>
              <xsl:otherwise>
                <xsl:text>(</xsl:text>
                <xsl:value-of select="$revisionflag-text"/>
                <xsl:text>)</xsl:text>
              </xsl:otherwise>
            </xsl:choose>
	  </xsl:if>

        </span>
        <br/>
      </xsl:otherwise>
    </xsl:choose>
  </span>

  <xsl:if test="$is.open != 0">
    <xsl:apply-templates select="tocentry">
      <xsl:with-param name="pageid" select="$pageid"/>
      <xsl:with-param name="relpath" select="$relpath"/>
    </xsl:apply-templates>
  </xsl:if>
</xsl:template>


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]