LANGUAGE DESIGN

構文と、文書構造と、出力を分ける。

Matraは、文書・データ・プログラムを共通の構造で扱うことを目指します。設計の起点は、sourceの書き方と文書値の表現、rendererによる出力を分けることです。現在のv0.3では、JavaScriptとの値の境界と文書ASTからHTMLへの経路を検証しています。技術書の補助資料として、基礎仕様と公開APIの契約を参照してください。

v0.3は実験版です。安定した互換性や一般的なsandbox保証を約束しません。

IMPLEMENTED PATH

JavaScriptの値から文書AST、HTMLへ

Node.js buildでは、Markdown中で入口とJavaScript値を指定し、Matraの実行結果をHTML rendererへ渡します。

左は入力Markdown文書の全体です。matra.entryがmain.matraを選び、matra.valuesのJavaScript式をcontentとして渡します。@matra/coreが文書を評価し、@matra/site-generationがページ・共通layout・データからサイトを生成します。CLIは新規プロジェクトの作成とbuild/previewに使います。下のコードは各packageの利用例です。

MARKDOWNexample.md
---
matra:
  entry: main.matra
  values:
    - content.js.part
---

# Compose a page with Matra packages

```main.matra
html(lang="en") {
  head {
    title { content.pageTitle(content.title) }
  }
  body {
    header {
      p { "MATRA / PACKAGE EXAMPLES" }
      h1 { content.title }
      p { content.description }
    }
    main {
      ...for (item in content.packages) {
        section(id=item.id) {
          h2 { item.name }
          p { item.description }
          pre { code { item.example } }
        }
      }
    }
    footer {
      p { "These package workflows are experimental; check each package's release notes." }
    }
  }
}
```

```content.js.part
({
  title: "Compose a page with Matra packages",
  description: "Use the CLI to start a project, Core to evaluate Matra documents, and site-generation to build a site.",
  pageTitle: title => `${title} | Matra`,
  packages: [
    {
      id: "cli",
      name: "@matra/cli",
      description: "Create and preview a starter project with the experimental CLI.",
      example: "npm install --global @matra/[email protected]\nmatra init hello\ncd hello && matra dev"
    },
    {
      id: "core",
      name: "@matra/core",
      description: "Build and run a literate Matra document in Node.js.",
      example: 'const value = await runLiterate(await buildLiterate(source))'
    },
    {
      id: "site-generation",
      name: "@matra/site-generation",
      description: "Generate pages from Matra templates, page data, and a shared layout.",
      example: 'await generateSite({ root, output: "dist", pages })'
    }
  ]
})
```
HTMLgenerated output
<html lang="en">
  <head>
    <title>Compose a page with Matra packages | Matra</title>
  </head>
  <body>
    <header>
      <p>MATRA / PACKAGE EXAMPLES</p>
      <h1>Compose a page with Matra packages</h1>
      <p>Use the CLI to start a project, Core to evaluate Matra documents, and site-generation to build a site.</p>
    </header>
    <main>
      <section id="cli">
        <h2>@matra/cli</h2>
        <p>Create and preview a starter project with the experimental CLI.</p>
        <pre><code>npm install --global @matra/[email protected]
matra init hello
cd hello &amp;&amp; matra dev</code></pre>
      </section>
      <section id="core">
        <h2>@matra/core</h2>
        <p>Build and run a literate Matra document in Node.js.</p>
        <pre><code>const value = await runLiterate(await buildLiterate(source))</code></pre>
      </section>
      <section id="site-generation">
        <h2>@matra/site-generation</h2>
        <p>Generate pages from Matra templates, page data, and a shared layout.</p>
        <pre><code>await generateSite({ root, output: &quot;dist&quot;, pages })</code></pre>
      </section>
    </main>
    <footer>
      <p>These package workflows are experimental; check each package&#39;s release notes.</p>
    </footer>
  </body>
</html>

設計の考え方

共通構造と、処理の境界。

01

tag・props・children

文書nodeとJSON互換valueをData Modelで定義し、ASTはobject形式、MatraJSONは交換形式として同じ文書構造を表します。sourceの構文木とは区別します。

Data Model →ASTと相互変換 →

02

parseと評価は別の操作

sourceをMatraModuleへparseする処理と、明示的に評価して値を得る処理を分けます。文書ASTへの変換やHTML描画も、それぞれ別の段階です。

parseと評価の契約 →literate API契約 →

03

構造的ラーニングという構想

他の言語を理解して共通構造へ取り込み、変換や再利用につなげる設計思想です。文書・データ・プログラムの統合を目指す長期構想であり、v0.3で完成した機能ではありません。

v0.3の技術的背景 →

何を検証するか

まずはコードと仕様を入口に。

現在のサイトは技術書の読者と、言語・ツールの実装に関心のある技術者を対象にしています。一般向けコンテンツの拡充はGUIビルダーの開発と合わせて検討します。GUIビルダーを提供済みとする案内ではありません。

v0.3の範囲を見る