emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* generating org headings from a source block
@ 2015-11-09 20:29 Matt Price
  2015-11-09 21:57 ` Nick Dokos
  0 siblings, 1 reply; 5+ messages in thread
From: Matt Price @ 2015-11-09 20:29 UTC (permalink / raw)
  To: Org Mode

[-- Attachment #1: Type: text/plain, Size: 2995 bytes --]

I would like to be able to insert into an org-buffer the text extracted
from a pdf file. PDF-Tools (https://github.com/politza/pdf-tools/) provides
some excellent tools for doing this.  I've written (well, msotly stolen) a
defun that finds all my highlights and returns them in the form of an org
heading:

(defun pdf-annot-export-as-org-heading (pdfpath)
  "Acquire highlight annotations as text, and insert into existing buffer
as org heading"
  (interactive)
  (let ((outputstring "")

        )
    (save-excursion
      (find-file pdfpath)
      (let (
            (annots (sort (pdf-annot-getannots nil (list 'highlight)  nil)
'pdf-annot-compare-annotations))
            (filename (format "%s.org"
                          (file-name-sans-extension
                           (buffer-name))))
        (linktext (concat "[[file://" (buffer-file-name)
                          "][" (file-name-sans-extension (buffer-name))
"]]" )))
      (setq outputstring (concat "** " (file-name-sans-extension
(buffer-name)) "\n\n"))

      ;; (insert (concat "#+TITLE: Notes for " (file-name-sans-extension
filename)))
      ;; (org-insert-heading-respect-content)
      ;; (insert (file-name-sans-extension filename))
      (mapc
       (lambda (annot) ;; traverse all annotations
         (message "%s" annot)
         (let ((page (assoc-default 'page annot))
               (text (assoc-default 'subject annot))
               )
           ;;(insert (concat "\n" text " (" linktext ", " (number-to-string
page) ")\n"))
           (setq outputstring (concat outputstring text " (" linktext ", "
(number-to-string page) ")\n\n"))
           )
         )
       annots)
      ))
    ;;(write-file filename t)
    outputstring
    ))

-------------

I'm sure it is very clumsy, but it sort of works.  I would like to be able
to call this function from a source block:

#+BEGIN_SRC elisp
(pdf-annot-export-as-org-heading
"/home/matt/HackingHistory/readings/latour-pandoras-hope.pdf")
(pdf-annot-export-as-org-heading
"/home/matt/HackingHistory/readings/historical-authority-hampton.pdf")
#+END_SRC

The results are close to, but not precisely, what I want:

#+RESULTS:
#+begin_example
** historical-authority-hampton

 ([[file:///home/matt/HackingHistory/readings/historical-authority-hampton.pdf][historical-authority-hampton]],
1)

In the Tudor palace at Hampton Court, there is a....
...
#+end_example

(a) I only get the last command, because I guess :results value only
reports the final returned value. But :results output gets me nothing.
What should I be doing?
(b) the whole output is wrapped in an example block, which I don't want.
Can I do something to fix this?
also, (c): I'd rather set the level of the org heading based on context.
Can I do that when I call from a source block? Should I maybe be doing this
some other way (e.g., jsut write an interactive function and call it with
M-x? But I like being able to assemble all the readings at one go, if
possible.

Thanks as always,
Matt

[-- Attachment #2: Type: text/html, Size: 3994 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: generating org headings from a source block
  2015-11-09 20:29 generating org headings from a source block Matt Price
@ 2015-11-09 21:57 ` Nick Dokos
  2015-11-10 14:28   ` Matt Price
  0 siblings, 1 reply; 5+ messages in thread
From: Nick Dokos @ 2015-11-09 21:57 UTC (permalink / raw)
  To: emacs-orgmode

Matt Price <moptop99@gmail.com> writes:

> I would like to be able to insert into an org-buffer the text extracted from a pdf file. PDF-Tools (
> https://github.com/politza/pdf-tools/) provides some excellent tools for doing this.  I've written
> (well, msotly stolen) a defun that finds all my highlights and returns them in the form of an org
> heading:
>
> (defun pdf-annot-export-as-org-heading (pdfpath)
>   ...)
> -------------
>
> I'm sure it is very clumsy, but it sort of works.  I would like to be able to call this function from a
> source block:
>
> #+BEGIN_SRC elisp
> (pdf-annot-export-as-org-heading "/home/matt/HackingHistory/readings/latour-pandoras-hope.pdf")
> (pdf-annot-export-as-org-heading "/home/matt/HackingHistory/readings/historical-authority-hampton.pdf")
> #+END_SRC
>
> The results are close to, but not precisely, what I want:
>
> #+RESULTS:
> #+begin_example
> ** historical-authority-hampton
>
>  ([[file:///home/matt/HackingHistory/readings/historical-authority-hampton.pdf]
> [historical-authority-hampton]], 1)
>
> In the Tudor palace at Hampton Court, there is a....
> ...
> #+end_example
>
> (a) I only get the last command, because I guess :results value only reports the final returned value.
> But :results output gets me nothing.  What should I be doing?

Have two source blocks? Or use :results output and output the string with (princ string)?

> (b) the whole output is wrapped in an example block, which I don't want.  Can I do something to fix
> this?

Maybe :results value raw  or :results value verbatim - untested. I can
never remember the right combo off the top of my head.

> also, (c): I'd rather set the level of the org heading based on context. Can I do that when I call from
> a source block? Should I maybe be doing this some other way (e.g., jsut write an interactive function
> and call it with M-x? But I like being able to assemble all the readings at one go, if possible. 
>

Pass the level as a parameter?

--
Nick

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: generating org headings from a source block
  2015-11-09 21:57 ` Nick Dokos
@ 2015-11-10 14:28   ` Matt Price
  2015-11-10 16:16     ` Nick Dokos
  0 siblings, 1 reply; 5+ messages in thread
From: Matt Price @ 2015-11-10 14:28 UTC (permalink / raw)
  Cc: Org Mode

[-- Attachment #1: Type: text/plain, Size: 3073 bytes --]

On Mon, Nov 9, 2015 at 4:57 PM, Nick Dokos <ndokos@gmail.com> wrote:

> Matt Price <moptop99@gmail.com> writes:
>
> > I would like to be able to insert into an org-buffer the text extracted
> from a pdf file. PDF-Tools (
> > https://github.com/politza/pdf-tools/) provides some excellent tools
> for doing this.  I've written
> > (well, msotly stolen) a defun that finds all my highlights and returns
> them in the form of an org
> > heading:
> >
> > (defun pdf-annot-export-as-org-heading (pdfpath)
> >   ...)
> > -------------
> >
> > I'm sure it is very clumsy, but it sort of works.  I would like to be
> able to call this function from a
> > source block:
> >
> > #+BEGIN_SRC elisp
> > (pdf-annot-export-as-org-heading
> "/home/matt/HackingHistory/readings/latour-pandoras-hope.pdf")
> > (pdf-annot-export-as-org-heading
> "/home/matt/HackingHistory/readings/historical-authority-hampton.pdf")
> > #+END_SRC
> >
> > The results are close to, but not precisely, what I want:
> >
> > #+RESULTS:
> > #+begin_example
> > ** historical-authority-hampton
> >
> >
>  ([[file:///home/matt/HackingHistory/readings/historical-authority-hampton.pdf]
> > [historical-authority-hampton]], 1)
> >
> > In the Tudor palace at Hampton Court, there is a....
> > ...
> > #+end_example
> >
> > (a) I only get the last command, because I guess :results value only
> reports the final returned value.
> > But :results output gets me nothing.  What should I be doing?
>
> Have two source blocks? Or use :results output and output the string with
> (princ string)?
>

I ended up with this.  It's not so bad, Though I use it enough that I
should probably just define another function. Also, I'm interested in how 2
source blocks would have worked...

#+BEGIN_SRC elisp :results output raw
  (let ((sources '(

"/home/matt/HackingHistory/readings/Troper-becoming-immigrant-city.pdf"
"/home/matt/HackingHistory/readings/historical-authority-hampton.pdf"))
        (output ""))
    (dolist (thispdf sources)
      (message "this pdf is: %s" thispdf)
      (setq output (concat output (pdf-annot-markups-as-org-text thispdf
))))
    (princ output))
#+END_SRC


>
> > (b) the whole output is wrapped in an example block, which I don't
> want.  Can I do something to fix
> > this?
>
> Maybe :results value raw  or :results value verbatim - untested. I can
> never remember the right combo off the top of my head.
>

:results value raw works -- thank you!

>
> > also, (c): I'd rather set the level of the org heading based on context.
> Can I do that when I call from
> > a source block? Should I maybe be doing this some other way (e.g., jsut
> write an interactive function
> > and call it with M-x? But I like being able to assemble all the readings
> at one go, if possible.
> >
>
> Pass the level as a parameter?
>

Can I pass the level of the current heading as a parameter, e.g.:
 #+BEGIN_SRC elisp :results output raw :var level=(1+ CURRENT-ORG-LEVEL)

where obviously CURRENT-ORG-LEVEL is some function I don't know how to
access?

Many thanks for your help, Nick!

>
> --
> Nick
>
>
>
>
>

[-- Attachment #2: Type: text/html, Size: 4590 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: generating org headings from a source block
  2015-11-10 14:28   ` Matt Price
@ 2015-11-10 16:16     ` Nick Dokos
  2015-11-10 16:31       ` Matt Price
  0 siblings, 1 reply; 5+ messages in thread
From: Nick Dokos @ 2015-11-10 16:16 UTC (permalink / raw)
  To: emacs-orgmode

Matt Price <moptop99@gmail.com> writes:

>     > also, (c): I'd rather set the level of the org heading based on context. Can I do that when I call
>     from
>     > a source block? Should I maybe be doing this some other way (e.g., jsut write an interactive
>     function
>     > and call it with M-x? But I like being able to assemble all the readings at one go, if possible. 
>     >
>    
>     Pass the level as a parameter?
>
> Can I pass the level of the current heading as a parameter, e.g.:
>  
>
> where obviously CURRENT-ORG-LEVEL is some function I don't know how to access?
>

You can at the very least set the level manually:

#+BEGIN_SRC elisp :results output raw :var level=3
...

by just eyeballing where the source block is in your file.
I think that's easy and does not require any programming.
You just have to remember to change the level when you cut
and paste the code block to other places.

There is org-current-level though if you want to go that way.

--
Nick

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: generating org headings from a source block
  2015-11-10 16:16     ` Nick Dokos
@ 2015-11-10 16:31       ` Matt Price
  0 siblings, 0 replies; 5+ messages in thread
From: Matt Price @ 2015-11-10 16:31 UTC (permalink / raw)
  Cc: Org Mode

[-- Attachment #1: Type: text/plain, Size: 759 bytes --]

On Tue, Nov 10, 2015 at 11:16 AM, Nick Dokos <ndokos@gmail.com> wrote:

>
> >     Pass the level as a parameter?
> >
> > Can I pass the level of the current heading as a parameter, e.g.:
> >
> > where obviously CURRENT-ORG-LEVEL is some function I don't know how to
> access?
> >
>
> You can at the very least set the level manually:
>
> #+BEGIN_SRC elisp :results output raw :var level=3
> ...
>
> by just eyeballing where the source block is in your file.
> I think that's easy and does not require any programming.
> You just have to remember to change the level when you cut
> and paste the code block to other places.
>
> There is org-current-level though if you want to go that way.
>
> :var level=(1+ (org-current-level))
works great.  Thank you Nick!

[-- Attachment #2: Type: text/html, Size: 1209 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2015-11-10 16:31 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-11-09 20:29 generating org headings from a source block Matt Price
2015-11-09 21:57 ` Nick Dokos
2015-11-10 14:28   ` Matt Price
2015-11-10 16:16     ` Nick Dokos
2015-11-10 16:31       ` Matt Price

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).