emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Nicolas Goaziou <n.goaziou@gmail.com>
To: Mark Shoulson <mark@kli.org>
Cc: emacs-orgmode@gnu.org
Subject: Re: Smart Quotes Exporting
Date: Tue, 19 Jun 2012 11:26:34 +0200	[thread overview]
Message-ID: <87ipenps8l.fsf@gmail.com> (raw)
In-Reply-To: <loom.20120615T171057-967@post.gmane.org> (Mark Shoulson's message of "Fri, 15 Jun 2012 16:20:43 +0000 (UTC)")

Hello,

Mark Shoulson <mark@kli.org> writes:

> Well, wait; regexps can make some pretty darn good guesses at the beginnings 
> or ends of strings.

I know that. They make a good job, I just want a better one.

> This isn't quite it; beginning-of-string followed by quote, then punctuation 
> and then spaces is also a close-quote, etc... There is a lot of fine-tuning.  
> But even what I currently have was able to handle your 
>
> Caesar said, "/Alea Jacta est./"
>
> example.

No, it doesn't handle that, actually, it's just sheer luck.  Indeed, the
quoting function is applied to "\"".  There's absolutely no space,
punctuation, etc. to save the day.  So it makes a wild guess with
a probability of 0.5 of success.  Since the guess is always the same,
"/a/" will always fail.

> The case where a quote both sits at the edge of a string (i.e. at the border 
> of some element, formatting, etc) *and* does not have whitespace next to it, 
> with possible punctuation, does not seem to be a normal occurrence to me.  If 
> I'm wrong, how common *is* it?

Even if it rarely happens, it can be _very_ annoying to have to cope
with bad guesses. If it can be avoided, I see no reason not to do so.

Now, here the infrastructure I propose.

Internally, the two following functions are required.

#+begin_src emacs-lisp
(defun org-export--smart-quotes-in-element (element backend)
  "Replace plain quotes with smart quotes in ELEMENT.

ELEMENT is an Org element or a secondary string.  BACKEND is the
back-end to check for rules, as a symbol.

This is a destructive operation.  Return new element."
  (let* ((type (org-element-type element))
         (properties (and type (nth 1 element))))
    ;; Destructively apply changes to secondary string, if any.
    (let ((secondary (and type (assq type org-element-secondary-value-alist))))
      (when secondary
        (let* ((sec-symbol (cdr secondary))
               (sec-value (plist-get properties sec-symbol)))
          (when sec-value
            (setq properties
                  (plist-put properties
                             sec-symbol
                             (org-export--smart-quotes-in-element
                              sec-value backend)))))))
    ;; Destructively change `:caption' if present.  Since it's a dual
    ;; keyword, apply smart quotes to both CAR and CDR, if required.
    (let ((caption (plist-get :caption properties)))
      (when caption
        (setq properties
              (plist-put properties
                         :caption
                         (cons
                          (org-export--smart-quotes-in-element
                           (car caption) backend)
                          (and (cdr caption)
                               (org-export--smart-quotes-in-element
                                (cdr caption) backend)))))))
    ;; Recursively apply changes to contents.  Rebuild ELEMENT along
    ;; the way, with updated strings.
    (let ((contents (if type (org-element-contents element) element))
          previous current next acc)
      (while contents
        (setq current (pop contents)
              next (car contents)
              previous current)
        (push
         (cond ((stringp current)
                ;; CURRENT is a string: Call
                ;; `org-export-quotation-marks' with appropriate
                ;; information.
                (org-export-quotation-marks
                 current
                 (and previous
                      (if (stringp previous)
                          (length (and (string-match " +\\'" previous)
                                       (match-string 0 previous)))
                        (org-element-property :post-blank previous)))
                 (and next
                      (if (not (stringp next)) 0
                        (length (and (string-match "\\` +" next)
                                     (match-string 0 next)))))
                 backend))
               ;; CURRENT is recursive: Move into it.
               ((plist-get properties :contents-begin)
                (org-export--smart-quotes-in-element current backend))
               ;; Otherwise, just accumulate CURRENT.
               (t current))
         acc))
      ;; Re-build transformed element.
      (if (or (not type) (eq type 'plain-text)) (nreverse acc)
        (nconc (list type properties) (nreverse acc))))))

(defun org-export-set-smart-quotes (tree backend info)
  "Replace plain quotes with smart quotes in TREE.

BACKEND is the back-end, as a symbol, used for transcoding.  INFO
is a plist used as a communication channel.

This is a destructive operation.  This function is meant to be
used as a parse tree filter for back-ends activating smart
quotes."
  ;; Destructively apply smart quotes to parsed keywords in info.
  (let ((value (plist-get info :title)))
    (when value
      (setq info
            (plist-put info
                       :title
                       (org-export--smart-quotes-in-element value backend)))))
  ;; Replace smart quotes in elements containing plain text or
  ;; secondary strings across the parse tree.
  (org-element-map
   tree '(paragraph verse-block table-cell headline inlinetask item)
   (lambda (el)
     (org-export-set-element el
                             (org-export--smart-quotes-in-element el backend))))
  ;; Return parse tree.
  tree)
#+end_src

Then, all is left to do is write the function replacing quotes in
a string, with additional information:

#+begin_src emacs-lisp
(defun org-export-quotation-marks (s &optional prev next backend)
  "Replace plain quotes with smart quotes in string S.

Optional argument PREV (resp. NEXT) is the number of white space
characters before (resp. after) the string, or nil if
S starts (resp. ends) a paragraph.

Optional argument BACKEND is a symbol representing the back-end
to use for substitutions.

The function returns the new string."
  ...)
#+end_src

Once this function is written, add `org-export-set-smart-quotes' as
a parse tree filter in `org-BACKEND-filters-alist'.

For example, one can add the following in org-e-latex.el to activate
smart quotes in latex export:

#+begin_src emacs-lisp
(defconst org-e-latex-filters-alist
  '((:filter-parse-tree . org-export-set-smart-quotes))
  "Alist between filters keywords and back-end specific filters.
See `org-export-filters-alist' for more information.")
#+end_src

Could you please try to modify your original
`org-export-quotation-marks' accordingly and test it?

>> Yes. You may want to use `org-element-at-point' and `org-element-type'
>> to tell if you're somewhere smart quotes are allowed (in table,
>> table-row, paragraph, verse-block elements).
>
> Probably.  I think I saw some other package make these decisions by peeking at 
> the formatting and seeing if it is set in comment-face or something, but 
> checking the element at point is presumably more sensible.

Thinking about it, looking at face used will definitely be faster,
though. That's your call.


Regards,

-- 
Nicolas Goaziou

  reply	other threads:[~2012-06-19  9:30 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-05-22  3:32 "Smart" quotes Mark E. Shoulson
2012-05-23 22:17 ` Nicolas Goaziou
2012-05-24  3:05   ` Mark E. Shoulson
2012-05-25 17:14     ` Nicolas Goaziou
2012-05-25 17:51       ` Jambunathan K
2012-05-25 22:51       ` Mark E. Shoulson
2012-05-26  6:48         ` Nicolas Goaziou
2012-05-29  1:30           ` Mark E. Shoulson
2012-05-29 17:57             ` Nicolas Goaziou
2012-05-30  0:51               ` Mark E. Shoulson
2012-05-31  1:50                 ` (no subject) Mark Shoulson
2012-05-31 13:38                   ` Nicolas Goaziou
2012-05-31 23:26                     ` Smart Quotes Exporting (Was: Re: (no subject)) Mark E. Shoulson
2012-06-01 17:11                       ` Smart Quotes Exporting Nicolas Goaziou
2012-06-01 22:41                         ` Mark E. Shoulson
2012-06-03  3:16                         ` Mark E. Shoulson
2012-06-06  2:14                         ` Mark E. Shoulson
2012-06-07 19:21                           ` Nicolas Goaziou
2012-06-11  1:28                             ` Mark Shoulson
2012-06-12 13:21                               ` Nicolas Goaziou
2012-06-15 16:20                                 ` Mark Shoulson
2012-06-19  9:26                                   ` Nicolas Goaziou [this message]
2012-08-07 23:18                                     ` Bastien

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87ipenps8l.fsf@gmail.com \
    --to=n.goaziou@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    --cc=mark@kli.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).