Re: [O] convert rmarkdown (rmd) files to orgmode?

2016-07-21 Thread Charles C. Berry

On Wed, 20 Jul 2016, Xebar Saram wrote:


Hi again

Chuck:

is there a way to modify you nice code regex code snippet to simultaneously
change all example blocks to R blocks in a converted rmd>org file?
so when executing the block all example blocks:


#+BEGIN_EXAMPLE
help.search("rnorm")
#+END_EXAMPLE

will turn into R blocks

#+BEGIN_SRC R :session Rorg  :results none
help.search("rnorm")
#+END_SRC



The obvious thing is to do this with two additional `replace' operations.

Interactively, you can use `replace-string' to do this, since those are 
fixed strings and there are no backreferences to worry about. I think 
there is a dired-mode incantation that would allow you to do this on all 
files in a directory if you wanted.


But see the docstring for 'replace-string' which shows a better 
incantation for programmatically doing the changes.


IMO, you would be better served by doiing this in a scripting language 
like bash or python, but even R could do it.


One reason for using a scripting language is that unless you are a wiz at 
elisp, it is easier to report back the changes that are made in context; 
if you convert something that truly *is* an example block, you want to 
know about it so you can back out that change. Also, it is possible that 
the R code in the file you are converting will contain characters that 
will foil the regexp matching and you really need to catch those.


I'd be tempted to do it in R - at least in part - so I could parse() the 
result to check that it is valid code. The ESS suite has some parsing 
capabilities, but if you do not already know them using R will be a lot 
easier. An example block that does not produce valid code might be 
something you should leave as an example block.


Write a script that does all the conversion including the pandoc 
bit. Then run that at the command line.


Chuck




Re: [O] convert rmarkdown (rmd) files to orgmode?

2016-07-20 Thread Xebar Saram
Hi again

Chuck:

is there a way to modify you nice code regex code snippet to simultaneously
change all example blocks to R blocks in a converted rmd>org file?
so when executing the block all example blocks:


 #+BEGIN_EXAMPLE
 help.search("rnorm")
 #+END_EXAMPLE

will turn into R blocks

 #+BEGIN_SRC R :session Rorg  :results none
 help.search("rnorm")
#+END_SRC


thx!

Z








On Wed, Jul 20, 2016 at 9:00 PM, Xebar Saram  wrote:

> ahh i see what you mean.
>
> thats a cool tip yet i find i have to find/replace also for images,
> find/replace to change example blocks to R code blocks etc which is quite
> tedious since i have about 50 of these converted Rmd to org files :)
> ill keep investigating this and report back
>
> thx so much for the tips! :)
>
> Z
>
> On Wed, Jul 20, 2016 at 8:42 PM, Charles C. Berry 
> wrote:
>
>> On Wed, 20 Jul 2016, Xebar Saram wrote:
>>
>> thx for the tips!
>>>
>>> when i try to run the source block :
>>>
>>> #+BEGIN_SRC emacs-lisp :results silent
>>>  (replace-regexp "^=[{]r \\([^}]*\\)[}]\\(.*\\)=$"
>>>  "#+name: \\1
>>>  ,#+begin_src R
>>>  \\2
>>>  ,#+end_src")
>>>
>>> #+END_SRC
>>>
>>> i just get a nil in the message area. what am i missing?
>>>
>>>
>>
>> Nothing!
>>
>> The `:results silent' should suppress any output.
>>
>> Anyway, what you should see is that the code chunks that pandoc placed
>> inside equal signs like
>>
>> : ={r my-name} code =
>>
>> are now in src blocks.
>>
>> I think you need to put the src block at the top or add a
>>
>> : (goto-char (point-min))
>>
>> as its first line to be sure it converts all the code chunks.
>>
>>
>> Chuck
>>
>
>


Re: [O] convert rmarkdown (rmd) files to orgmode?

2016-07-20 Thread Xebar Saram
ahh i see what you mean.

thats a cool tip yet i find i have to find/replace also for images,
find/replace to change example blocks to R code blocks etc which is quite
tedious since i have about 50 of these converted Rmd to org files :)
ill keep investigating this and report back

thx so much for the tips! :)

Z

On Wed, Jul 20, 2016 at 8:42 PM, Charles C. Berry  wrote:

> On Wed, 20 Jul 2016, Xebar Saram wrote:
>
> thx for the tips!
>>
>> when i try to run the source block :
>>
>> #+BEGIN_SRC emacs-lisp :results silent
>>  (replace-regexp "^=[{]r \\([^}]*\\)[}]\\(.*\\)=$"
>>  "#+name: \\1
>>  ,#+begin_src R
>>  \\2
>>  ,#+end_src")
>>
>> #+END_SRC
>>
>> i just get a nil in the message area. what am i missing?
>>
>>
>
> Nothing!
>
> The `:results silent' should suppress any output.
>
> Anyway, what you should see is that the code chunks that pandoc placed
> inside equal signs like
>
> : ={r my-name} code =
>
> are now in src blocks.
>
> I think you need to put the src block at the top or add a
>
> : (goto-char (point-min))
>
> as its first line to be sure it converts all the code chunks.
>
>
> Chuck
>


Re: [O] convert rmarkdown (rmd) files to orgmode?

2016-07-20 Thread Charles C. Berry

On Wed, 20 Jul 2016, Xebar Saram wrote:


thx for the tips!

when i try to run the source block :

#+BEGIN_SRC emacs-lisp :results silent
 (replace-regexp "^=[{]r \\([^}]*\\)[}]\\(.*\\)=$"
 "#+name: \\1
 ,#+begin_src R
 \\2
 ,#+end_src")

#+END_SRC

i just get a nil in the message area. what am i missing?




Nothing!

The `:results silent' should suppress any output.

Anyway, what you should see is that the code chunks that pandoc placed 
inside equal signs like


: ={r my-name} code =

are now in src blocks.

I think you need to put the src block at the top or add a

: (goto-char (point-min))

as its first line to be sure it converts all the code chunks.


Chuck



Re: [O] convert rmarkdown (rmd) files to orgmode?

2016-07-20 Thread Xebar Saram
thx for the tips!

when i try to run the source block :

#+BEGIN_SRC emacs-lisp :results silent
  (replace-regexp "^=[{]r \\([^}]*\\)[}]\\(.*\\)=$"
  "#+name: \\1
  ,#+begin_src R
  \\2
  ,#+end_src")

#+END_SRC

i just get a nil in the message area. what am i missing?

thx

Z

On Wed, Jul 20, 2016 at 8:20 PM, Charles C. Berry  wrote:

> On Wed, 20 Jul 2016, Xebar Saram wrote:
>
> thx phil
>>
>> the Rmd format is actually quite different than md so that conversion
>> didnt
>> go well
>>
>>
> I tried this
>
> pandoc -f markdown -t org input-file.Rmd -o output-file.org
>
> then I opened `output-file.org' and put this src block at the very top:
>
> #+BEGIN_SRC emacs-lisp :results silent
>   (replace-regexp "^=[{]r \\([^}]*\\)[}]\\(.*\\)=$"
>   "#+name: \\1
>   ,#+begin_src R
>   \\2
>   ,#+end_src")
>
> #+END_SRC
>
> When I execute that code block, all the converted code chunks become src
> blocks.
>
> This isn't perfect as chunk options are appended to the `#+NAME:...' line,
> but if you want to play with the regexp's you can probably get it to pick
> those out and put them on a separate line. Or just write another src block
> with another `replace-regexp' to fix those lines.
>
> With a little effort you can write a command file for `sed' to do what the
> code block above does and then pipe the pandoc output to that command like
> this:
>
> : pandoc -f markdown -t org input-file.Rmd | \
> : sed -f convert-chunks > output-file.org
>
> and you have an org document ready (or almost ready) to go.
>
>
> HTH,
>
> Chuck
>
>


Re: [O] convert rmarkdown (rmd) files to orgmode?

2016-07-20 Thread Charles C. Berry

On Wed, 20 Jul 2016, Xebar Saram wrote:


thx phil

the Rmd format is actually quite different than md so that conversion didnt
go well



I tried this

pandoc -f markdown -t org input-file.Rmd -o output-file.org

then I opened `output-file.org' and put this src block at the very top:

#+BEGIN_SRC emacs-lisp :results silent
  (replace-regexp "^=[{]r \\([^}]*\\)[}]\\(.*\\)=$"
  "#+name: \\1
  ,#+begin_src R
  \\2
  ,#+end_src")

#+END_SRC

When I execute that code block, all the converted code chunks become src 
blocks.


This isn't perfect as chunk options are appended to the `#+NAME:...' line, 
but if you want to play with the regexp's you can probably get it to pick 
those out and put them on a separate line. Or just write another src block 
with another `replace-regexp' to fix those lines.


With a little effort you can write a command file for `sed' to do what the 
code block above does and then pipe the pandoc output to that command like 
this:


: pandoc -f markdown -t org input-file.Rmd | \
: sed -f convert-chunks > output-file.org

and you have an org document ready (or almost ready) to go.


HTH,

Chuck




Re: [O] convert rmarkdown (rmd) files to orgmode?

2016-07-20 Thread Xebar Saram
thx phil

the Rmd format is actually quite different than md so that conversion didnt
go well

i ended up doing a 2 step conversion:

in R (via the GUI or through R -e command line), first convert the Rmd
files to md files:

require(knitr) # required for knitting from rmd to md
require(markdown) # required for md to html
knit('test.rmd', 'test.md') # creates md file

then use pandoc to convert the md files to org:

pandoc -o test.org  test.md

this gives an almost 1:1 org file


thx

Z

On Wed, Jul 20, 2016 at 1:08 PM, Philip Hudson 
wrote:

> pandoc -f markdown -t org myfile.rmd
>
> On 20 July 2016 at 05:28, Xebar Saram  wrote:
> > Hi all
> >
> > anyone know of a way to convert rmarkdown (rmd) files to orgmode?
> >
> > im preparing a R course and lots of cool examples in R are in rmd format
> so
> > it could be very useful to me
> >
> > best
> >
> > Z
>
>
>
> --
> Phil Hudson   http://hudson-it.ddns.net
> @UWascalWabbit PGP/GnuPG ID: 0x887DCA63
>