From a71e95bf4f07e044eb3f335929d2adea74247a9e Mon Sep 17 00:00:00 2001 From: Minijackson Date: Sun, 22 Dec 2019 18:10:39 +0100 Subject: Fix TOC links for inline documentation --- src/parser/clang/entities.rs | 61 ++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 34 deletions(-) (limited to 'src/parser/clang/entities.rs') diff --git a/src/parser/clang/entities.rs b/src/parser/clang/entities.rs index 41fe061..cbc17b7 100644 --- a/src/parser/clang/entities.rs +++ b/src/parser/clang/entities.rs @@ -1,4 +1,4 @@ -use crate::types::*; +use crate::types::{self, *}; use clang::{EntityKind, Usr}; use thiserror::Error; @@ -169,55 +169,48 @@ where } } +fn entity_id(usr: Usr) -> EntityId { + // This is a somewhat ugly workaround, because Pandoc parses markdown identifiers as alphanum + // or one of "-_:.", which means Pandoc can't parse libclang's USRs in title ids and related. + // + // + EntityId(usr.0.replace("@", "::").replace("#", ".").replace("$", "-")) +} + +fn append_children( + acc: &mut types::Children, + to_insert: BTreeMap>, +) { + for (usr, child) in to_insert { + acc.entry(ChildrenKind(String::from( + child.entity.kind().to_str_plural(), + ))) + .or_default() + .insert(entity_id(usr), child.into()); + } +} + impl From> for Entity { fn from(entity: Described) -> Self { - let mut children: BTreeMap> = BTreeMap::new(); + let mut children: types::Children = BTreeMap::new(); let kind = EntityKind(String::from(entity.entity.kind().to_str_singular())); let clang_children = entity.entity.into_children(); if let Some(namespaces) = clang_children.namespaces { - for (usr, namespace) in namespaces { - children - .entry(ChildrenKind(String::from( - namespace.entity.kind().to_str_plural(), - ))) - .or_default() - .insert(EntityId(usr.0), namespace.into()); - } + append_children(&mut children, namespaces); } if let Some(variables) = clang_children.variables { - for (usr, variable) in variables { - children - .entry(ChildrenKind(String::from( - variable.entity.kind().to_str_plural(), - ))) - .or_default() - .insert(EntityId(usr.0), variable.into()); - } + append_children(&mut children, variables); } if let Some(structs) = clang_children.structs { - for (usr, r#struct) in structs { - children - .entry(ChildrenKind(String::from( - r#struct.entity.kind().to_str_plural(), - ))) - .or_default() - .insert(EntityId(usr.0), r#struct.into()); - } + append_children(&mut children, structs); } if let Some(functions) = clang_children.functions { - for (usr, function) in functions { - children - .entry(ChildrenKind(String::from( - function.entity.kind().to_str_plural(), - ))) - .or_default() - .insert(EntityId(usr.0), function.into()); - } + append_children(&mut children, functions); } Entity { -- cgit v1.2.3