0
0

asciidocの図や表の番号を章番号に合わせる

Last updated at Posted at 2024-07-19

色々調べたが、どうもadocファイル単体では難しそうということがわかった。

javascriptを埋め込んでhtml表示時に置換するのが一番楽っぽい

adocファイル

sectnums,figure-caption,table-captionを指定する。
scriptは生成されるhtmlからの相対位置にあるものを指定する。

# SomeTitle
:toc:
:toc-levels: 3
:toc-title: 目次
:nofooter:
:sectnums:
:figure-caption: 図
:table-caption: 表
:stylesheet: asciidoc.css

++++
<script src="scripts/custom.js"></script>
++++

...<snip>

javascriptファイル

図は<div class="title">タグに記載されている。
表は<caption class="title">タグに記載されている。
余計なものも交じるので弾くようにする。

章番号は<div class="sect1">毎の<h2 id="xxxxx">にあるので
後は章毎に番号をincrementして置換していくだけ。

document.addEventListener('DOMContentLoaded', function () {
    var figures = document.querySelectorAll('div.title');
    var tables = document.querySelectorAll('caption.title');

    function updateCaptions(elements, type) {
        var sect_cnt = {};
        elements.forEach(function (element) {
            let before = element.innerText;
            // console.log(`${before}`);
            if (!before.match(`^${type} `)) {
                return;
            }
                
            // console.log(element + " " + element.innerText);
            const ancestor = element.closest("div.sect1");
            const sect = ancestor.querySelector('h2');
            const snum = sect.innerText.replace(/\..*$/, '').trim();
            if (!sect_cnt[snum]) {
                sect_cnt[snum] = 0;
            }
            sect_cnt[snum]++;
            const counter = sect_cnt[snum];
            const after = before.replace(/\d+/, `${snum}.${counter}`);
            element.innerText = after;
            // console.log(after);
        });
    }

    updateCaptions(figures, '');
    updateCaptions(tables, '');
});

0
0
0

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
0
0