LoginSignup
2

More than 5 years have passed since last update.

NimでNimの実行結果をマークダウンに出力する

Posted at

概要

Nimのプログラムとその実行結果をMarkdownに出力するツールです。
まったく俺得&Adventカレンダーの穴埋め用。

いわゆるサンプルコードとその結果を表示するアレ

sample.nim
echo "hello"
hello

こういうコードと実行結果をいちいち貼り付けるのも面倒だなーと思い、サンプルコードを

  • そのまま実行するタスク
  • コードブロックごとにコンパイルして、実行結果(標準出力)を拾い、Markdownを作成するタスク

といった作業をVSCodeのタスクとして用意して、気軽にテストコードの実行&Markdownを生成するツールを作ってみました。

サンプルコードの形式

サンプルコードは、3つのパートから構成されます。

  • 概要部
  • import/共通部
  • コードブロック(複数)

import/共通部と、各コードブロックを結合し、テンポラリファイルへ出力後、nim c -rで実行しています。

以下サンプルコード

# =============================
# overview: NimのXXX処理をコツコツと
# 説明1
# 説明2
# 説明3

# =============================
# imports: インポートモジュール
import json,strutils
import os
const message = "hello"

# =============================
# title: XXXX1処理
block:
    echo "test1 " & message

# =============================
# title: XXXX2処理
block:
    echo "test2 " & message

# =============================
# title: XXXX3処理
block:
    echo "test3 " & message

このソースをMarkdownで出力するとこんなカンジになります。
ブロックの区切りは、# =============================になっていますw

# 概要
NimのXXX処理をコツコツと
説明1
説明2
説明3

## 以下メモ

### XXXX1処理

"""nim
import json,strutils
import os
const message = "hello"

block:
    echo "test1  " & message
"""

"""shell-session
(stdout)
test1 hello
"""

### XXXX2処理

"""nim
import json,strutils
import os
const message = "hello"

block:
    echo "test2 " & message
"""

"""shell-session
(stdout)
test2 hello
"""

### XXXX3処理

"""nim
import json,strutils
import os
const message = "hello"

block:
    echo "test3 " & message
"""

"""shell-session
(stdout)
test3 hello
"""

以下、出力サンプル

どっかで見かけたことありますね。

概要

NimのXXX処理をコツコツと
説明1
説明2
説明3

以下メモ

XXXX1処理

import json,strutils
import os
const message = "hello"

block:
    echo "test1" & message
(stdout)
test1 hello

XXXX2処理

import json,strutils
import os
const message = "hello"

block:
    echo "test2" & message
(stdout)
test2 hello

XXXX3処理

import json,strutils
import os
const message = "hello"

block:
    echo "test3" & message
(stdout)
test3 hello

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2