From mboxrd@z Thu Jan 1 00:00:00 1970 From: Nicolas Goaziou Subject: Re: Configurable prefixes for heading-tree numbering in HTML export? ($) Date: Sun, 31 Jul 2016 00:06:35 +0200 Message-ID: <87oa5edbmc.fsf@saiph.selenimh> References: Mime-Version: 1.0 Content-Type: text/plain Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:52148) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bTcP4-000145-Jr for emacs-orgmode@gnu.org; Sat, 30 Jul 2016 18:06:44 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bTcOz-0001e8-5K for emacs-orgmode@gnu.org; Sat, 30 Jul 2016 18:06:41 -0400 Received: from relay4-d.mail.gandi.net ([217.70.183.196]:33604) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bTcOy-0001dn-R9 for emacs-orgmode@gnu.org; Sat, 30 Jul 2016 18:06:37 -0400 In-Reply-To: (D. C. Toedt's message of "Thu, 28 Jul 2016 09:15:19 -0500") List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: "Emacs-orgmode" To: "D. C. Toedt" Cc: emacs-orgmode@gnu.org Hello, "D. C. Toedt" writes: > So far as I can tell, org-mode currently allows heading numbering in only > one style (1, 1.1, 1.1.1, etc.). For HTML export, I'm interested in having > the heading numbering be configurable on a per-subtree basis, along > something like the following lines for a hypothetical consulting-agreement > contract (indentation is for convenient reading only). This is for my Common > Draft contract terms and conditions project > --- I'd like to be able to arrange the trees in an arbitrary order and have > the top-level headings start with prefixes and heading numbers instead of > heading numbers alone. > > This can be done in a very kludgy fashion with CSS (see below), but doing > it in org-mode would be far preferable for several reasons. > > I'd be happy to pay an honorarium, or make a donation, of USD$100-$200 for > the appropriate elisp code that I could include in the relevant file(s). > I'd want the elisp code to be open-sourced, and at least minimal > documentation, so that it could be made a candidate for possible inclusion > in a future org-mode release. > > Here's an example of the desired org-mode code: > > > ===BEGIN=== > > > * Services > > :PROPERTIES: > > :CUSTOM_ID: SVC > > :CUSTOM_PREFIX: t > > :END: > # ======== NOTE THE :CUSTOM_PREFIX: property above =========== # > > > ** Statements of Work > > [properties and text omitted] > > > *** Written Statements of Work Required > > [properties and text omitted] > # =========== AN ARBITRARY NUMBER OF SUBHEADING LEVELS BENEATH THE TOP > LEVEL ============= # > > > *** Changes Must Be in Writing > [properties and text omitted] > > > > ** Billing Rates > > [properties and text omitted] > > > ** IP Ownership > > [properties and text omitted] > > > * General Provisions > > :PROPERTIES: > > :CUSTOM_ID: GP > :CUSTOM_PREFIX: t > > :END: > > > ** Amendments > > [properties and text omitted] > > > ** Notices > > [properties and text omitted] > > ===END=== > > > I'd like for the resulting HTML export to be something like the following: > > SVC Services > > # ======== NOTE THAT THERE'S NO HEADING /NUMBER/ FOR THE TOP-LEVEL HEADING, > JUST THE PREFIX ========== # > > > SVC-1 Statements of Work > > [properties and text omitted] > > # ======== PREFERABLY THE SEPARATOR IN "SVC-1" COULD BE CONFIGURED AS A > HYPHEN, A PERIOD, A COLON, A FORWARD SLASH, ETC. ============= # > > > SVC-1.1 Written Statements of Work Required > > [properties and text omitted] > > SVC-1.2 Changes Must Be in Writing > > [properties and text omitted] > > > SVC-2 Billing Rates > > [properties and text omitted] > > > SVC-3 IP Ownership > > [properties and text omitted] > > > GP General Provisions > > > GP-1 Amendments > > [properties and text omitted] > > > GP-2 Notices > > [properties and text omitted] > Possibly not what you're asking but, you can ignore CUSTOM_PREFIX property altogether and use a headline filter to do what you want. (defun my-heading-prefix-filter (heading backend info) "Prefix every heading and sub-heading with custom ID. Skip one level of numbering." (when (org-export-derived-backend-p backend 'html) (let ((internal-ref (and (string-match "\\`.*id=\"outline-container-\\(.+?\\)\"" heading) (match-string 1 heading))) (id (and (string-match "\\`.*\n" heading) (match-string 1 heading)))) ;; When there is no custom ID, id attribute contains the ;; internal reference. So we check if there is a custom ID which ;; is going to become the prefix of all headings. (unless (equal internal-ref id) ;No custom ID (let ((s 0)) (while (string-match "\\(\\)\\(?:.*?\\(.+?\\)\\)?" heading s) (setq s (+ (match-end 0) (length id) 1)) (setq heading (cond ;; Unnumbered heading. ((not (match-end 2)) (replace-match (concat "\\1" id " ") nil nil heading 1)) ;; Top level heading. Ignore numbering. ((string-match-p "\\`[0-9]+\\'" (match-string 2 heading)) (replace-match id nil nil heading 2)) ;; Sub-headings. Skip first number. (t (let ((numbers (save-match-data (split-string (match-string 2 heading) "\\.")))) (replace-match (concat id "-" (mapconcat #'identity (cdr numbers) ".")) nil nil heading 2)))))) heading))))) (add-to-list 'org-export-filter-headline-functions 'my-heading-prefix-filter) custom ID is used to prefix every heading with its value. Of course, it means that every heading with a custom ID is expected to follow this pattern, so you cannot toggle the feature subtree wise. Anyway that's a starting point. Regards, -- Nicolas Goaziou