星际文件系统-使用XSLT的简单IPFS博客

有一个问题:IPFS中的站点无法使用服务器端脚本来形成页面。如果在加载之前使用页面生成,然后向每个页面添加新的菜单项,我们将更改这些页面的哈希值。因此,整个页面的组装必须由浏览器完成。


通常使用JavaScript来形成页面的内容。这是一种熟悉的技术,但是有其缺点。


我将使用XSLT。这是一种古老的模板技术,早已内置于浏览器中,但很少有人使用它。可能是由于模板与命名空间的混淆以及许多错误而没有清晰的解释,从而使您编写了大量文本。同样,尽管浏览器中已经存在XSLT 3.0,但仍然只有XSLT 1.0可用。


XSLT的工作方式如下:


  1. 用户在浏览器中打开XML文档。
  2. 在XML标头中,文档包含指向XSLT模板的链接。
    <?xml-stylesheet href="xslt/.xslt" type="text/xsl" ?>
  3. 浏览器中基于XML文档和其他数据的模板形成一个xHTML文档。
  4. 浏览器将显示生成的xHTML文档。

通过将多个页面链接到一个模板,您可以更改显示的xHTML文档,而无需更改XML文档。因此,在更改设计时,XML文档的哈希不会更改,这意味着它们的旧副本将成为IPFS中新副本的源。


对于搜索引擎,此方法也具有优势。它们仅限于处理XML文档,仅接收唯一的页面内容,而没有导航元素和在每个页面上重复的其他块。


图片



您可以使用XML中的任何标签。最主要的是不违反XML语法,该语法比HTML语法稍严格:


  1. 所有标签必须关闭。
    但是可以使用自封闭标签<br />
  2. 顶层只能有一个标签。其余标签必须包含在其中。
    <>
        < />
        <- />
        <-- />
    </>
  3. 文本只能在标签内。
    <>
         
    </>

新entry.xml


我们的任务是使博客对于用户而言简单易懂,他将能够使用简单的文本编辑器独立地对其进行补充。


用于用户完成的XML博客条目模板:
文件: “ new entry.xml”


<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="xslt/.xslt" type="text/xsl" ?>
<>
    <></>
    <>
        <></>
    </>
</>
<!--         -->

. . meta description. .


xHTML ( HTML ) .


:


: 'Lorem Ipsum.xml'
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="xslt/.xslt" type="text/xsl" ?>
<>
    <>Lorem Ipsum</>
    <>
<h1>Lorem ipsum dolor sit amet</h1>

<>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras feugiat orci non orci tincidunt molestie. Donec eget porta metus. Aenean malesuada nunc lacus, a sollicitudin neque fermentum eu. <b style="color: green">Vestibulum egestas viverra urna</b>, sit amet mollis ipsum commodo imperdiet. Quisque sit amet est eu nibh commodo pharetra.</> Donec fermentum nisi nec lorem auctor imperdiet. Morbi ultricies at magna ullamcorper malesuada. In eget arcu dapibus, vehicula nunc vel, convallis erat. Cras accumsan lacus vel tellus gravida, eu gravida turpis congue. In ultrices pellentesque odio at hendrerit.

<h1>Cras nec lorem facilisis</h1>

Cras nec lorem facilisis justo porttitor scelerisque id id ipsum. Duis mattis, sem eu sollicitudin pharetra, diam augue ullamcorper nisl, id dictum turpis ex in nisl. Aliquam venenatis egestas urna eget porttitor. Suspendisse potenti. Curabitur fermentum fermentum nulla, a lobortis ipsum laoreet eget. Duis finibus lacus id tempor elementum. <i>Interdum et malesuada fames ac ante ipsum primis in faucibus</i>. Curabitur tempus eget turpis ut tincidunt. In quis massa et risus tempor luctus quis eu enim. Duis finibus sem sit amet elit scelerisque molestie. Suspendisse laoreet, leo ut accumsan dapibus, nisi tellus dignissim lorem, eu auctor tellus diam vel nisi. Donec sem tortor, suscipit a leo maximus, aliquet dapibus nisl. Integer sodales, dui ut vulputate pharetra, ipsum neque tempor ipsum, sed porta tortor quam a nisl.

Sed consequat purus sed porttitor pulvinar. Mauris sollicitudin non sem a malesuada. Vestibulum non velit finibus, elementum purus nec, aliquam lacus. Etiam leo enim, venenatis id luctus ut, ornare sit amet ex. Nunc vel fringilla ante. Cras lobortis scelerisque pulvinar. Ut lectus augue, viverra quis molestie eu, maximus nec leo. <u>Sed facilisis odio ut malesuada mollis</u>. Pellentesque enim lacus, luctus nec velit quis, scelerisque tincidunt magna. Proin laoreet, erat eget porttitor bibendum, odio augue suscipit purus, id commodo magna nibh sed est. Ut dignissim aliquet sem, id suscipit sapien mollis volutpat. Vivamus ac fringilla est, at sagittis diam.
    </>
</>

.xhtml


xHTML XSLT . . XSLT .


xHTML XML.


: 'xhtml/.xhtml'


<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <!--   <>     title    -->
        <title class="">   XML</title>
        <meta  class=""/>
        <!--       HTML -->
        <style>
            /*   xHTML    */

            .{
                white-space: pre-wrap;
            }

            /*       */

              . h1, . h2
            , . h3, . h4
            , . h5, . h6 {
                display: inline;
            }
        </style>
    </head>
    <body>
        <!--           -->
        <ul   class="" />
        <h1   class="" />
        <span class=""> <b style="color: red"><![CDATA[<></>]]></b>   .  .</span>
    </body>
</html>

.xslt


(XML xHTML) XSLT.


'.xhtml' XML. XML xHTML.


: 'xslt/.xslt'


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
  version="1.0"
  xmlns="http://www.w3.org/1999/xhtml"
  xmlns:xhtml="http://www.w3.org/1999/xhtml"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <!--
                '.xslt'
              
        Chrome     URL   URL
    -->
    <xsl:include href="%D0%BD%D0%B0%D0%B2%D0%B8%D0%B3%D0%B0%D1%86%D0%B8%D1%8F.xslt" />

    <!--      xHTML     XML -->
    <xsl:output method="xml" encoding="UTF-8"/>

    <!--        -->
    <xsl:variable name="" select="" />

    <!--     '.xhtml' -->
    <xsl:template match="/">
        <!-- mode            -->
        <xsl:apply-templates mode="xhtml" select="document('../xhtml/%D0%B7%D0%B0%D0%BF%D0%B8%D1%81%D1%8C.xhtml')" />
    </xsl:template>

    <!--        -->
    <xsl:template mode="xhtml" match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates mode="xhtml" select="node()|@*"/>
        </xsl:copy>
    </xsl:template>

    <!--         '.xhtml' -->

    <!--   match    Firefox -->

    <!--    ''     '' -->
    <xsl:template mode="xhtml" match="*[contains(@class, '')]">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates mode="xml" select="$////node()" />
        </xsl:copy>
    </xsl:template>

    <!--   meta    '' -->
    <xsl:template mode="xhtml" match="xhtml:meta[contains(@class, '')]">
        <meta name="description" content="{$///}" />
    </xsl:template>

    <!--       -->
    <xsl:template mode="xhtml" match="*[contains(@class, '')]">
        <!--       '.xsl' -->
        <xsl:call-template name="" />
    </xsl:template>

    <!--    ''     '' -->
    <xsl:template mode="xhtml" match="*[contains(@class, '')]">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:value-of select="$/" />
        </xsl:copy>
    </xsl:template>

    <!--    ''     '' -->
    <xsl:template mode="xhtml" match="*[contains(@class, '')]">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates mode="xml" select="$//node()" />
        </xsl:copy>
    </xsl:template>

    <!-- 
              xml 

              
        (   svg )  
     -->
    <xsl:template mode="xml" match="*[namespace-uri()]|@*">
        <xsl:copy>
            <xsl:apply-templates mode="xml" select="node()|@*" />
        </xsl:copy>
    </xsl:template>

    <!--       -->
    <xsl:template mode="xml" match="*">
        <!--
                 .    
                 
        -->
        <xsl:element name="{name()}">
            <xsl:apply-templates mode="xml" select="node()|@*" />
        </xsl:element>
    </xsl:template>

    <!--    ''    -->
    <xsl:template mode="xml" match="">
        <xsl:apply-templates mode="xml" select="node()|@*" />
    </xsl:template>

    <!--      xHTML  --> 
    <xsl:template mode="xml" match="h1|h2|h3|h4|h5">
        <!-- 
            h1       '' 
               xHTML  
        -->
        <xsl:element name="h{substring(name(), 2, 1) + 1}">
            <!--          id     -->
            <xsl:attribute name="id">
                <xsl:value-of select="." />
            </xsl:attribute>
            <xsl:apply-templates mode="xml" select="node()|@*" />
        </xsl:element>
    </xsl:template>

</xsl:stylesheet>


. .


.xml


XML :


: '.xml'


<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="xslt/.xslt" type="text/xsl" ?>
<>
    <> </>
</>
<!--         -->

'' xml .


:


<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="xslt/.xslt" type="text/xsl" ?>
<>
    <>Lorem Ipsum 1</>
    <>Lorem Ipsum 2</>
</>


'.xml' xHTML .


: 'xslt/.xslt'


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
  version="1.0"
  xmlns="http://www.w3.org/1999/xhtml"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <!--      '.xsl' -->
    <xsl:template name="">
        <ul>
            <!--   -->
            <xsl:copy-of select="@*" />

            <!--     '.xml'   -->
            <xsl:apply-templates mode="" select="document('../%D0%B6%D1%83%D1%80%D0%BD%D0%B0%D0%BB.xml')//" />
        </ul>
    </xsl:template>

    <!--       -->
    <xsl:template mode="" match="">
        <li>
            <a href="{text()}.xml">
                <xsl:value-of select="text()" />
            </a>
        </li>
    </xsl:template>

</xsl:stylesheet>

.



.


index.html


IPFS index.html. XML .


'.xml'.


: 'index.html'


<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
        <!--     '.xml' -->
        <!-- Firefox      url   -->
        <meta http-equiv="refresh" content="0; url=%D0%B6%D1%83%D1%80%D0%BD%D0%B0%D0%BB.xml" />
    </head>
    <body>
        <!--        -->
         :
        "<a class="" href=".xml"></a>"
    </body>
</html>

HTML . XML HTML . .


.xslt


'.xml' '.xslt'. '.xml' 'index.html' .


: 'xslt/.xslt'


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
  version="1.0"
  xmlns="http://www.w3.org/1999/xhtml"
  xmlns:xhtml="http://www.w3.org/1999/xhtml"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="xml" encoding="UTF-8"/>

    <xsl:variable name="" select="//text()" />

    <xsl:template match="/">
        <!--       -->
        <xsl:apply-templates mode="html" select="document('../index.html')" />
    </xsl:template>

    <!--     -->
    <xsl:template mode="html" match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates mode="html" select="node()|@*" />
        </xsl:copy>
    </xsl:template>

    <!--    -->

    <!--     -->
    <xsl:template mode="html" match="xhtml:meta[@http-equiv='refresh']">
        <!--
             
             1.   URL
                      URL
        -->
        <base href="{$}.xml" />

        <!-- 2.   -->
        <meta http-equiv="refresh" content="0; url=#" />
    </xsl:template>

    <!--         -->
    <xsl:template mode="html" match="xhtml:a[@class='']">
        <a href="" class="{@class}">
            <xsl:value-of select="$" />
        </a>
    </xsl:template>

</xsl:stylesheet>

'.xslt' .



  1. '.xml' :


    <?xml version="1.0" encoding="UTF-8"?>
    <?xml-stylesheet href="xslt/.xslt" type="text/xsl" ?>
    <>
        <>  </>
    </>

  2. ' .xml' ' .xml'


  3. :


    <?xml version="1.0" encoding="UTF-8"?>
    <?xml-stylesheet href="xslt/.xslt" type="text/xsl" ?>
    <>
        <>  </>
        <>
       .
        </>
    </>


.



IPFS .


Firefox


  1. about:config security.fileuri.strict_origin_policy false

Chrome


  1. --allow-file-access-from-files

IPFS


.


ID


.


ipfs key gen -t rsa blog

ID



  1. ipfs


    ipfs add -r -s rabin --inline --raw-leaves --fscache < >

    -r
    -s rabin — jpeg, mp3 .
    --inline — .
    --raw-leaves--nocopy .
    --fscache — .


  2. ID .


    ipfs name publish -k blog <>



:


http://127.0.0.1:8080/ipfs/<>
http://127.0.0.1:8080/ipns/<id >

:


http://gateway.ipfs.io/ipfs/<>
http://gateway.ipfs.io/ipns/<id >

. ID .


.



自然,可以并且应该开发此Blog的设计和功能以满足用户的需求。我为进一步发展奠定了基础。


参考文献


关于XSLT
GitHub代码的好参考快照


All Articles