{"id":3241,"date":"2016-07-06T20:19:54","date_gmt":"2016-07-07T00:19:54","guid":{"rendered":"http:\/\/fgiasson.com\/blog\/?p=3241"},"modified":"2017-02-17T10:39:02","modified_gmt":"2017-02-17T15:39:02","slug":"literate-clojure-programming-using-org-mode","status":"publish","type":"post","link":"https:\/\/fgiasson.com\/blog\/index.php\/2016\/07\/06\/literate-clojure-programming-using-org-mode\/","title":{"rendered":"Literate [Clojure] Programming Using Org-mode"},"content":{"rendered":"<p>Literate Programming is a great way to write computer software, particularly in fields like data science where data processing workflows are complex and often need much background information. I started to write about Literate Programming a few months ago, and now it is the time to formalize how I create Literate Programming applications.<\/p>\n<p>This blog post belong to a series of posts about Literate [Clojure] Programming:<\/p>\n<ol class=\"org-ol\">\n<li><a href=\"https:\/\/fgiasson.com\/blog\/index.php\/2016\/06\/21\/optimal-emacs-settings-for-org-mode-for-literate-programming\/\">Configuring Emacs for Org-mode<\/a><\/li>\n<li><a href=\"https:\/\/fgiasson.com\/blog\/index.php\/2016\/07\/06\/literate-clojure-programming-using-org-mode\/\">Project folder structure<\/a> (this post)<\/li>\n<li><a href=\"https:\/\/fgiasson.com\/blog\/index.php\/2016\/08\/11\/literate-clojure-programming-anatomy-of-a-org-mode-file\/\">Anatomy of a Org-mode file<\/a><\/li>\n<li><a href=\"https:\/\/fgiasson.com\/blog\/index.php\/2016\/10\/26\/literate-clojure-programming-tangle-all-in-org-mode\/\">Tangling all project files<\/a><\/li>\n<li><a href=\"https:\/\/fgiasson.com\/blog\/index.php\/2017\/02\/17\/literate-clojure-programming-publishing-documentation-in-multiple-formats\/\">Publishing documentation in multiple formats<\/a><\/li>\n<li><a href=\"https:\/\/fgiasson.com\/blog\/index.php\/2016\/05\/30\/creating-and-running-unit-tests-directly-in-source-files-with-org-mode\/\">Unit Testing<\/a><\/li>\n<\/ol>\n<p><!--more--><\/p>\n<div id=\"outline-container-orgheadline1\" class=\"outline-2\">\n<h3 id=\"orgheadline1\">Clojure Project Folder Structure<\/h3>\n<div id=\"text-orgheadline1\" class=\"outline-text-2\">\n<p>The structure of a programming project can vary a lot. The structure I am using when developing Clojure is the one created by <a href=\"http:\/\/leiningen.org\/\">Leiningen<\/a> which I use for creating and managing my Clojure projects. The structure of a simple project (in this case, the <a href=\"https:\/\/github.com\/structureddynamics\/org-mode-clj-tests-utils\">org-mode-clj-tests-utils<\/a> project that I created for another blog post) looks like this:<\/p>\n<pre class=\"example\">- CHANGELOG.md\n- LICENCE\n- README.md\n- resources\n- pom.xml\n- project.clj\n- src\n  - org_mode_clj_tests_utils\n    - core.clj\n- target\n- test\n  - org_mode_clj_tests_utils\n    - core_test.clj\n<\/pre>\n<p>There are 4 main components to this structure:<\/p>\n<ol class=\"org-ol\">\n<li>the <code>project.clj<\/code> file which is used by Leiningen to configure the project<\/li>\n<li>the <code>src<\/code> folder where the project&#8217;s code files [to be compiled] are located<\/li>\n<li>the <code>target<\/code> folder is where the compiled files will be available, and<\/li>\n<li>the <code>test<\/code> folder where the unit tests for the code sources are located<\/li>\n<\/ol>\n<p>This kind of project outline is really simple and typical. Now let&#8217;s see what the structure would look like if this project would be created using Literate [Clojure] Programming.<\/p>\n<\/div>\n<\/div>\n<div id=\"outline-container-orgheadline2\" class=\"outline-2\">\n<h3 id=\"orgheadline2\">Literate Clojure Folder Structure<\/h3>\n<div id=\"text-orgheadline2\" class=\"outline-text-2\">\n<p>The best way and cleanest way I found to create and manage the Org-mode files is to create a <code>org<\/code> directory at the same level as the <code>src<\/code> one. Then to replicate the same folder structure that exists in the <code>src<\/code> folder. The names of the source files should be the same except that they have the <code>.org<\/code> file extension. For example, the <code>src\/core.clj<\/code> file would become <code>org\/core.org<\/code> in the Org-mode folder, and the <code>org\/core.org<\/code> file is used to <i>tangle<\/i> (create) the <code>src\/core.clj<\/code> file.<\/p>\n<p>The new structure would look like that:<\/p>\n<pre class=\"example\">- CHANGELOG.md\n- LICENCE\n- README.md\n- resources\n- org\n  - project.org\n  - org_mode_clj_tests_utils\n    - core.org\n- pom.xml\n- project.clj\n- src\n  - org_mode_clj_tests_utils\n    - core.clj\n- target\n- test\n  - org_mode_clj_tests_utils\n    - core_test.clj\n<\/pre>\n<p>The idea here is that all the files that needs to be modified related to the project would become a Org-mode file. Such files are the code source files, the test files, possible other documentation files and the <code>project.clj<\/code> file. When the Org-mode files will be tangled, then all the appropriate files, required by the Clojure project would be generated.<\/p>\n<p>Anything I am writing for this project comes from a Org-mode file. All the development occurs in Org-mode. If someone would want to modify such a Literate Clojure application, then they would have to modify the Org-mode file and not the source files otherwise the changes would be overwritten by the next tangling operation.<\/p>\n<\/div>\n<\/div>\n<div id=\"outline-container-orgheadline3\" class=\"outline-2\">\n<h3 id=\"orgheadline3\">Utilities Org-mode Files<\/h3>\n<div id=\"text-orgheadline3\" class=\"outline-text-2\">\n<p>Finally, I created a series of Org-mode files that are used to perform special tasks such as:<\/p>\n<ol class=\"org-ol\">\n<li>Tangling all project files at once, and<\/li>\n<li>Publishing documentation in multiple formats<\/li>\n<\/ol>\n<p>These are Org-mode files that can be executed to perform these tasks. In the case of tangling all project files at once, it would be necessary to use it if you haven&#8217;t changed the behavior of your Emacs to automatically tangle files on save.<\/p>\n<p>The second file is to publish <code>weaved<\/code> documentation in multiple different formats (HTML, LaTeX, etc.) as required, all at once.<\/p>\n<p>These two files are directly located into the <code>\/org\/<\/code> folder. I will explain how they work in a subsequent post in that series. The final structure of a Literate Clojure project is:<\/p>\n<pre class=\"example\">- CHANGELOG.md\n- LICENCE\n- README.md\n- resources\n- org\n  - project.org\n  - publish.org\n  - tangle-all.org\n  - setup.org\n  - org_mode_clj_tests_utils\n    - core.org\n- pom.xml\n- project.clj\n- src\n  - org_mode_clj_tests_utils\n    - core.clj\n- target\n- test\n  - org_mode_clj_tests_utils\n    - core_test.clj\n<\/pre>\n<\/div>\n<\/div>\n<div id=\"outline-container-orgheadline4\" class=\"outline-2\">\n<h3 id=\"orgheadline4\">Conclusion<\/h3>\n<div id=\"text-orgheadline4\" class=\"outline-text-2\">\n<p>As you can see, a Literate Clojure application is not much different. The way to program such an application is more profound than the small changes that occur at the level of the folder structure.<\/p>\n<p>There is still an open question related to publishing this kind of Literate work on repositories such as Git: should only the <code>org<\/code> folder be added to a Git repository, or should we also add the files that get tangled as well? In an ideal World, only the <code>org<\/code> files would need to go into the repository. However, depending on the nature of work (work only accessible by you, or work accessible by a group of people that know Org-mode, or making the project public on GitHub, etc.) we may have to commit the tangled files too. In the case of an open source project, I think it is required since many people unfamiliar with Org-mode won&#8217;t be able to use the codebase because they won&#8217;t be able to tangle it from the Org files. For this specific reason, I tend to publish the <code>org<\/code> files along with all the files that get tangled from them. That way I am sure that even if the users of the library doesn&#8217;t know anything about Org-mode or Literate Programming they could still use the code. The only thing I try to take care of is to commit the Org file and the tangled file related to a specific change in the same commit, and I try not to create two commits, one for each file.<\/p>\n<p>The next blog post of that series will explain how the Org-mode source files are actually created, what is their internal structure, how they are organized and used.<\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Literate Programming is a great way to write computer software, particularly in fields like data science where data processing workflows are complex and often need much background information. I started to write about Literate Programming a few months ago, and now it is the time to formalize how I create Literate Programming applications. This blog [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[251,279,277,66],"tags":[252,254,274,276],"class_list":["post-3241","post","type-post","status-publish","format-standard","hentry","category-clojure","category-emacs","category-literate-programming","category-programming","tag-clojure-2","tag-emacs","tag-literateprogramming","tag-orgmode"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/fgiasson.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3241","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fgiasson.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fgiasson.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fgiasson.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/fgiasson.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=3241"}],"version-history":[{"count":11,"href":"https:\/\/fgiasson.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3241\/revisions"}],"predecessor-version":[{"id":3617,"href":"https:\/\/fgiasson.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3241\/revisions\/3617"}],"wp:attachment":[{"href":"https:\/\/fgiasson.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=3241"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fgiasson.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=3241"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fgiasson.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=3241"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}