Skip to content

Commit

Permalink
fix big toc bug with toplevel 2
Browse files Browse the repository at this point in the history
  • Loading branch information
cirosantilli committed Dec 27, 2019
1 parent a7ed52d commit 3473e7f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
2 changes: 2 additions & 0 deletions README.ciro
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ To also show the section auto-generated number as in "Section X.Y My title" we a
Read this \x[my-id]{style=full}.
]]

Example: \x[cross-reference-style]{style=full}.

\h[3][Internal cross file references]

TODO
Expand Down
30 changes: 23 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1042,12 +1042,14 @@ function parse(tokens, macros, options, extra_returns={}) {
// * the insane but necessary paragraphs double newline syntax
// * automatic ul parent to li and table to tr
// * extract all IDs into an ID index
let todo_visit = [ast_toplevel];
let macro_counts = {};
const todo_visit = [ast_toplevel];
const macro_counts = {};
let header_graph_last_level;
const header_graph_stack = new Map();
let first_header = true;
let first_header_level;
extra_returns.context.ids = {};
let header_graph_last_level = 0;
extra_returns.context.header_graph = new TreeNode();
let header_graph_stack = {0: extra_returns.context.header_graph};
extra_returns.context.has_toc = false;
while (todo_visit.length > 0) {
const ast = todo_visit.shift();
Expand Down Expand Up @@ -1091,16 +1093,30 @@ function parse(tokens, macros, options, extra_returns={}) {
if (macro_name === Macro.HEADER_MACRO_NAME) {
let level = parseInt(convert_arg_noescape(ast.args.level, id_context));
ast.level = level;
if (first_header) {
first_header = false;
first_header_level = level;
header_graph_last_level = level - 1;
header_graph_stack[header_graph_last_level] = extra_returns.context.header_graph;
}
let new_tree_node = new TreeNode(ast, header_graph_stack[level - 1]);
ast.header_tree_node = new_tree_node;
if (((level - header_graph_last_level) > 1) && (header_graph_last_level != 0)) {
if (level - header_graph_last_level > 1) {
parse_error(
state,
`skipped a header level from ${header_graph_last_level} to ${level}`,
ast.args.level[0].line,
ast.args.level[0].column
);
}
if (level < first_header_level) {
parse_error(
state,
`header level ${level} is smaller than the level of the first header of the document ${first_header_level}`,
ast.args.level[0].line,
ast.args.level[0].column
);
}
let parent_tree_node = header_graph_stack[level - 1];
if (parent_tree_node !== undefined) {
parent_tree_node.add_child(new_tree_node);
Expand Down Expand Up @@ -1197,9 +1213,9 @@ function parse(tokens, macros, options, extra_returns={}) {
// Calculate header_graph_top_level.
let level0_header = extra_returns.context.header_graph;
if (level0_header.children.length === 1) {
extra_returns.context.header_graph_top_level = level0_header.children[0].value.level;
extra_returns.context.header_graph_top_level = first_header_level;
} else {
extra_returns.context.header_graph_top_level = 0;
extra_returns.context.header_graph_top_level = first_header_level - 1;
}

return ast_toplevel;
Expand Down

0 comments on commit 3473e7f

Please sign in to comment.