Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

example code: "table of content" with redcloth/zazen #66

Open
testbird opened this issue Nov 13, 2013 · 0 comments
Open

example code: "table of content" with redcloth/zazen #66

testbird opened this issue Nov 13, 2013 · 0 comments

Comments

@testbird
Copy link

The following snippet from
http://snippets.aktagon.com/snippets/48-generate-a-toc-for-a-textile-file
has a generate_toc method, probably usefull to auto-render a table of
contents for a zazen "ToC." paragraph,, or a tag?

The adapted headreg expression should parse the
textile IDs from headings, e.g. "h2(#ID)." :

headreg = /^\s*h([1-6])#?\((.*)\)\.\s+(.*)/

def generate_toc (file_name, headreg)
    document = IO.read(file_name)
    toc = "" 
    document.gsub(headreg) do |match|
        number = $1
        id = $2
        title = $3
        toc << '#' * number.to_i + ' "' + title + '":#' + id +
"\n" end
    RedCloth.new(toc).to_html
end

To manually assign descriptive IDs to headers, one may explicitly write them like
"h2(#ch1). Chapter 1".
Redcloth already renders this as
<h2 id=“ch1”>Chapter 1</h2>
(using id for anchors is ok, html5 even dropped named <a> tags)
and it can be cross referenced as "":#ch1

Another possibility may be auto IDs on saving:

All headers without a given ID, for example,
h2. Chapter 1
may automatically get expanded with a random not yet used 4-5 character
string, and get saved for example as
h2(#45i4t). Chapter 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant