- "Ensure characters are properly escaped when used in headlines or blocks.
-
-Escape characters are: @ { }"
- (replace-regexp-in-string "\\\([@{}]\\\)" "@\\1" text))
-
-;;; Menu creation
-
-(defun org-texinfo--build-menu (tree level info &optional detailed)
- "Create the @menu/@end menu information from TREE at headline
-level LEVEL.
-
-TREE contains the parse-tree to work with, either of the entire
-document or of a specific parent headline. LEVEL indicates what
-level of headlines to look at when generating the menu. INFO is
-a plist containing contextual information.
-
-Detailed determines whether to build a single level of menu, or
-recurse into all children as well."
- (let ((menu (org-texinfo--generate-menu-list tree level info))
- output text-menu)
- (cond
- (detailed
- ;; Looping is done within the menu generation.
- (setq text-menu (org-texinfo--generate-detailed menu level info)))
- (t
- (setq text-menu (org-texinfo--generate-menu-items menu info))))
- (when text-menu
- (setq output (org-texinfo--format-menu text-menu))
- (mapconcat 'identity output "\n"))))
-
-(defun org-texinfo--generate-detailed (menu level info)
- "Generate a detailed listing of all subheadings within MENU starting at LEVEL.
-
-MENU is the parse-tree to work with. LEVEL is the starting level
-for the menu headlines and from which recursion occurs. INFO is
-a plist containing contextual information."
- (when level
- (let ((max-depth (min org-texinfo-max-toc-depth
- (plist-get info :headline-levels))))
- (when (> max-depth level)
- (loop for headline in menu append
- (let* ((title (org-texinfo--menu-headlines headline info))
- ;; Create list of menu entries for the next level
- (sublist (org-texinfo--generate-menu-list
- headline (1+ level) info))
- ;; Generate the menu items for that level. If
- ;; there are none omit that heading completely,
- ;; otherwise join the title to it's related entries.
- (submenu (if (org-texinfo--generate-menu-items sublist info)
- (append (list title)
- (org-texinfo--generate-menu-items sublist info))
- 'nil))
- ;; Start the process over the next level down.
- (recursion (org-texinfo--generate-detailed sublist (1+ level) info)))
- (setq recursion (append submenu recursion))
- recursion))))))
-
-(defun org-texinfo--generate-menu-list (tree level info)
- "Generate the list of headlines that are within a given level
-of the tree for further formatting.
-
-TREE is the parse-tree containing the headlines. LEVEL is the
-headline level to generate a list of. INFO is a plist holding
-contextual information."
- (org-element-map tree 'headline
- (lambda (head)
- (and (= (org-export-get-relative-level head info) level)
- ;; Do not take note of footnotes or copying headlines.
- (not (org-element-property :COPYING head))
- (not (org-element-property :footnote-section-p head))
- ;; Collect headline.
- head))
- info))
-
-(defun org-texinfo--generate-menu-items (items info)
- "Generate a list of headline information from the listing ITEMS.
-
-ITEMS is a list of the headlines to be converted into entries.
-INFO is a plist containing contextual information.
-
-Returns a list containing the following information from each
-headline: length, title, description. This is used to format the
-menu using `org-texinfo--format-menu'."
- (loop for headline in items collect
- (let* ((menu-title (org-texinfo--sanitize-menu
- (org-export-data
- (org-export-get-alt-title headline info)
- info)))
- (title (org-texinfo--sanitize-menu
- (org-texinfo--sanitize-headline
- (org-element-property :title headline) info)))
- (descr (org-export-data
- (org-element-property :DESCRIPTION headline)
- info))
- (menu-entry (if (string= "" menu-title) title menu-title))
- (len (length menu-entry))
- (output (list len menu-entry descr)))
- output)))
-
-(defun org-texinfo--menu-headlines (headline info)
- "Retrieve the title from HEADLINE.
-
-INFO is a plist holding contextual information.
-
-Return the headline as a list of (length title description) with
-length of -1 and nil description. This is used in
-`org-texinfo--format-menu' to identify headlines as opposed to
-entries."
- (let ((title (org-export-data
- (org-element-property :title headline) info)))
- (list -1 title 'nil)))
-
-(defun org-texinfo--format-menu (text-menu)
- "Format the TEXT-MENU items to be properly printed in the menu.
-
-Each entry in the menu should be provided as (length title
-description).
-
-Headlines in the detailed menu are given length -1 to ensure they
-are never confused with other entries. They also have no
-description.
-
-Other menu items are output as:
- Title:: description
-
-With the spacing between :: and description based on the length
-of the longest menu entry."
-
- (let (output)
- (setq output
- (mapcar (lambda (name)
- (let* ((title (nth 1 name))
- (desc (nth 2 name))
- (length (nth 0 name))
- (column (max
- ;;6 is "* " ":: " for inserted text
- length
- (-
- org-texinfo-node-description-column
- 6)))
- (spacing (- column length)
- ))
- (if (> length -1)
- (concat "* " title ":: "
- (make-string spacing ?\s)
- (if desc
- (concat desc)))
- (concat "\n" title "\n"))))
- text-menu))
- output))