reddit / emacs

Import php files into org document as separate code blocks

kolerezooi · 2022-08-14 07:30
I want to add a bunch of php files that are in a directory into an org document as separate code blocks. This is what I want to achieve: #+begin_src html :tangle "/sudo::/srv/http/01.php" <?php // code of 01.php ?> #+end_src #+begin_src html :tangle "sudo::/srv/http/02.php" <?php // code of 02.php ?> #+end_src The idea is to have an org document with the php code imbedded. Then it is easy to create the website from one org document by running org-babel-tangle. Right now I'm using a bash code block to create results: #+name: php-files #+BEGIN_SRC bash :results output cd $HOME/Downloads/; for f in *; do echo "#+begin_src html :tangle '/sudo::/srv/http/$f'" paste /dev/null - < "$f" #pukes out php content of file echo "#+end_src" echo "" done #+end_src #+RESULTS: php-files #+begin_example ,#+begin_src html :tangle '/sudo::/srv/http/01.php' <?php // code of 01.php ?> ,#+end_src ,#+begin_src html :tangle '/sudo::/srv/http/02.php' <?php // code of 01.php } ?> #+end_example This output is almost what I want. Except the comma prefixes ([default behavior of code blocks](https://orgmode.org/manual/Literal-Examples.html#Literal-Examples) ) and single quote instead of double quote. (the double quotes can be generated by the bash script. I will figure this out later). At the moment I am using a keyboard macro to remove the , and overwrite ' with ". This can also be done by evil mode. My question is if there are other ways to create these code block outputs? Preferably the code should be part of the org document, to make the document portable.
JohnathanDee · 2022-08-14 18:23
Hrmmm... Dunno if there's an "Emacs way", but somewhat similar scenario I wrote a Python script to generate a PHP array from an Org table. You're going in the opposite direction, but the idea is still the same: write a script that generates your Org file from separate PHP files, then going forward you only need to change your Org file and tangle. Different languages have Org parsing packages, too, but in your case you only need to automate the block header and footer. Could script it in bash
_viz_ · 2022-08-15 11:04
You're looking for :results raw. #+begin_src sh :results raw output echo \#+BEGIN_SRC test \"some attribute\" echo \#+END_SRC #+end_src #+RESULTS: #+BEGIN_SRC test "some attribute" #+END_SRC
kolerezooi · 2022-08-16 19:35
Thanks! That's almost too easy :-)