I'd like to import a source code file (for a language such as e.g. python) into an org-mode file so that the comments in the source file become org-mode text (markdown) and the codes between comments become code blocks of the same language. Basically, this converts a source code into a Literate Programming format. It is in a sense "org file extraction" and the inverse of tangling.

Is there an automatic (or close to automatic) way to do such conversions in org-mode?

(This is with org-mode 9.4.6, Emacs 27, Ubuntu 20.04)

  • 2
    Not that I know of. But if your program is a sequence of comment blocks and code blocks (IOW no inline comments need be considered), then a fairly simple awk program (e.g.) should be able to produce the Org mode file: you just switch between two states (in-comment and in-code) and in each state you read as much of the file as conforms to the current state; when you get to the part that does not conform, you output the block (with some transformations, e.g. strip comment markers in one case or surround the block with source code markers in the other), switch state and continue.
    – NickD
    Jan 8 at 23:19
  • 2
    I would look into some way to achieve this with hops. For example, if you can convert Python to RST, you could then use Pandoc to convert RST to Org. ?
    – mankoff
    Jan 9 at 0:12
  • 2
    ... and could this help? slott56.github.io/PyLit-3/_build/html/index.html
    – mankoff
    Jan 9 at 0:13
  • Thanks. Your comments are very helpful.
    – tinlyx
    Jan 9 at 1:25


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.