ox-taskjuggler.el 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  1. ;;; ox-taskjuggler.el --- TaskJuggler Back-End for Org Export Engine
  2. ;;
  3. ;; Copyright (C) 2007-2013 Free Software Foundation, Inc.
  4. ;;
  5. ;; Emacs Lisp Archive Entry
  6. ;; Filename: ox-taskjuggler.el
  7. ;; Author: Christian Egli
  8. ;; Nicolas Goaziou <n dot goaziou at gmail dot com>
  9. ;; Maintainer: Christian Egli
  10. ;; Keywords: org, taskjuggler, project planning
  11. ;; Description: Converts an Org mode buffer into a TaskJuggler project plan
  12. ;; This file is not part of GNU Emacs.
  13. ;; This program is free software: you can redistribute it and/or modify
  14. ;; it under the terms of the GNU General Public License as published by
  15. ;; the Free Software Foundation, either version 3 of the License, or
  16. ;; (at your option) any later version.
  17. ;; This program is distributed in the hope that it will be useful,
  18. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. ;; GNU General Public License for more details.
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. ;;; Commentary:
  24. ;;
  25. ;; This library implements a TaskJuggler exporter for Org mode.
  26. ;; TaskJuggler is a project planing tool that uses a text format to
  27. ;; define projects, tasks and resources, so it is a natural fit for
  28. ;; Org mode. It can produce all sorts of reports for tasks or
  29. ;; resources in either HTML, CSV or PDF. TaskJuggler is implemented
  30. ;; in Ruby and should therefore run on any platform.
  31. ;;
  32. ;; The exporter does not export all the nodes of a document or
  33. ;; strictly follow the order of the nodes in the document.
  34. ;;
  35. ;; Instead the TaskJuggler exporter looks for a tree that defines the
  36. ;; tasks and a optionally tree that defines the resources for this
  37. ;; project. It then creates a TaskJuggler file based on these trees
  38. ;; and the attributes defined in all the nodes.
  39. ;;
  40. ;; * Installation
  41. ;;
  42. ;; Put this file into your load-path and the following line into your
  43. ;; ~/.emacs:
  44. ;;
  45. ;; (add-to-list 'org-export-backends 'taskjuggler)
  46. ;;
  47. ;; or customize `org-export-backends' variable.
  48. ;;
  49. ;; The interactive functions are the following:
  50. ;;
  51. ;; M-x `org-taskjuggler-export'
  52. ;; M-x `org-taskjuggler-export-and-open'
  53. ;;
  54. ;; * Tasks
  55. ;;
  56. ;; Let's illustrate the usage with a small example. Create your tasks
  57. ;; as you usually do with org-mode. Assign efforts to each task using
  58. ;; properties (it's easiest to do this in the column view). You
  59. ;; should end up with something similar to the example by Peter Jones
  60. ;; in:
  61. ;;
  62. ;; http://www.contextualdevelopment.com/static/artifacts/articles/2008/project-planning/project-planning.org.
  63. ;;
  64. ;; Now mark the top node of your tasks with a tag named
  65. ;; "taskjuggler_project" (or whatever you customized
  66. ;; `org-taskjuggler-project-tag' to). You are now ready to export the
  67. ;; project plan with `org-taskjuggler-export-and-open' which will
  68. ;; export the project plan and open a Gantt chart in TaskJugglerUI.
  69. ;;
  70. ;; * Resources
  71. ;;
  72. ;; Next you can define resources and assign those to work on specific
  73. ;; tasks. You can group your resources hierarchically. Tag the top
  74. ;; node of the resources with "taskjuggler_resource" (or whatever you
  75. ;; customized `org-taskjuggler-resource-tag' to). You can optionally
  76. ;; assign an identifier (named "resource_id") to the resources (using
  77. ;; the standard org properties commands) or you can let the exporter
  78. ;; generate identifiers automatically (the exporter picks the first
  79. ;; word of the headline as the identifier as long as it is unique, see
  80. ;; the documentation of `org-taskjuggler--build-unique-id'). Using that
  81. ;; identifier you can then allocate resources to tasks. This is again
  82. ;; done with the "allocate" property on the tasks. Do this in column
  83. ;; view or when on the task type
  84. ;;
  85. ;; C-c C-x p allocate RET <resource_id> RET
  86. ;;
  87. ;; Once the allocations are done you can again export to TaskJuggler
  88. ;; and check in the Resource Allocation Graph which person is working
  89. ;; on what task at what time.
  90. ;;
  91. ;; * Export of properties
  92. ;;
  93. ;; The exporter also takes TODO state information into consideration,
  94. ;; i.e. if a task is marked as done it will have the corresponding
  95. ;; attribute in TaskJuggler ("complete 100"). Also it will export any
  96. ;; property on a task resource or resource node which is known to
  97. ;; TaskJuggler, such as limits, vacation, shift, booking, efficiency,
  98. ;; journalentry, rate for resources or account, start, note, duration,
  99. ;; end, journalentry, milestone, reference, responsible, scheduling,
  100. ;; etc for tasks.
  101. ;;
  102. ;; * Dependencies
  103. ;;
  104. ;; The exporter will handle dependencies that are defined in the tasks
  105. ;; either with the ORDERED attribute (see TODO dependencies in the Org
  106. ;; mode manual) or with the BLOCKER attribute (see org-depend.el) or
  107. ;; alternatively with a depends attribute. Both the BLOCKER and the
  108. ;; depends attribute can be either "previous-sibling" or a reference
  109. ;; to an identifier (named "task_id") which is defined for another
  110. ;; task in the project. BLOCKER and the depends attribute can define
  111. ;; multiple dependencies separated by either space or comma. You can
  112. ;; also specify optional attributes on the dependency by simply
  113. ;; appending it. The following examples should illustrate this:
  114. ;;
  115. ;; * Training material
  116. ;; :PROPERTIES:
  117. ;; :task_id: training_material
  118. ;; :ORDERED: t
  119. ;; :END:
  120. ;; ** Markup Guidelines
  121. ;; :PROPERTIES:
  122. ;; :Effort: 2d
  123. ;; :END:
  124. ;; ** Workflow Guidelines
  125. ;; :PROPERTIES:
  126. ;; :Effort: 2d
  127. ;; :END:
  128. ;; * Presentation
  129. ;; :PROPERTIES:
  130. ;; :Effort: 2d
  131. ;; :BLOCKER: training_material { gapduration 1d } some_other_task
  132. ;; :END:
  133. ;;
  134. ;;;; * TODO
  135. ;; - Look at org-file-properties, org-global-properties and
  136. ;; org-global-properties-fixed
  137. ;; - What about property inheritance and org-property-inherit-p?
  138. ;; - Use TYPE_TODO as an way to assign resources
  139. ;;
  140. ;;; Code:
  141. (eval-when-compile (require 'cl))
  142. (require 'ox)
  143. ;;; User Variables
  144. (defgroup org-export-taskjuggler nil
  145. "Options specific for TaskJuggler export back-end."
  146. :tag "Org Export TaskJuggler"
  147. :group 'org-export)
  148. (defcustom org-taskjuggler-extension ".tjp"
  149. "Extension of TaskJuggler files."
  150. :group 'org-export-taskjuggler
  151. :type 'string)
  152. (defcustom org-taskjuggler-project-tag "taskjuggler_project"
  153. "Tag marking project's tasks.
  154. This tag is used to find the tree containing all the tasks for
  155. the project."
  156. :group 'org-export-taskjuggler
  157. :type 'string)
  158. (defcustom org-taskjuggler-resource-tag "taskjuggler_resource"
  159. "Tag marking project's resources.
  160. This tag is used to find the tree containing all the resources
  161. for the project."
  162. :group 'org-export-taskjuggler
  163. :type 'string)
  164. (defcustom org-taskjuggler-report-tag "taskjuggler_report"
  165. "Tag marking project's reports.
  166. This tag is used to find the tree containing all the reports for
  167. the project."
  168. :group 'org-export-taskjuggler
  169. :type 'string)
  170. (defcustom org-taskjuggler-target-version 3.0
  171. "Which version of TaskJuggler the exporter is targeting.
  172. By default a project plan is exported which conforms to version
  173. 3.x of TaskJuggler. For a project plan that is compatible with
  174. versions of TaskJuggler older than 3.0 set this to 2.4.
  175. If you change this variable be sure to also change
  176. `org-taskjuggler-default-reports' as the format of reports has
  177. changed considerably between version 2.x and 3.x of TaskJuggler"
  178. :group 'org-export-taskjuggler
  179. :type 'number)
  180. (defcustom org-taskjuggler-default-project-version "1.0"
  181. "Default version string for the project.
  182. This value can also be set with the \":VERSION:\" property
  183. associated to the headline defining the project."
  184. :group 'org-export-taskjuggler
  185. :type 'string)
  186. (defcustom org-taskjuggler-default-project-duration 280
  187. "Default project duration.
  188. The value will be used if no start and end date have been defined
  189. in the root node of the task tree, i.e. the tree that has been
  190. marked with `org-taskjuggler-project-tag'"
  191. :group 'org-export-taskjuggler
  192. :type 'integer)
  193. (defcustom org-taskjuggler-default-reports
  194. '("textreport report \"Plan\" {
  195. formats html
  196. header '== <-query attribute=\"name\"-> =='
  197. center -8<-
  198. [#Plan Plan] | [#Resource_Allocation Resource Allocation]
  199. ----
  200. === Plan ===
  201. <[report id=\"plan\"]>
  202. ----
  203. === Resource Allocation ===
  204. <[report id=\"resourceGraph\"]>
  205. ->8-
  206. }
  207. # A traditional Gantt chart with a project overview.
  208. taskreport plan \"\" {
  209. headline \"Project Plan\"
  210. columns bsi, name, start, end, effort, chart
  211. loadunit shortauto
  212. hideresource 1
  213. }
  214. # A graph showing resource allocation. It identifies whether each
  215. # resource is under- or over-allocated for.
  216. resourcereport resourceGraph \"\" {
  217. headline \"Resource Allocation Graph\"
  218. columns no, name, effort, weekly
  219. loadunit shortauto
  220. hidetask ~(isleaf() & isleaf_())
  221. sorttasks plan.start.up
  222. }")
  223. "Default reports for the project.
  224. These are sensible default reports to give a good out-of-the-box
  225. result when exporting without defining any reports. If you want
  226. to define your own reports you can change them here or simply
  227. define the default reports so that they include an external
  228. report definition as follows:
  229. include reports.tji
  230. These default are made to work with tj3. If you are targeting
  231. TaskJuggler 2.4 (see `org-taskjuggler-target-version') please
  232. change these defaults to something like the following:
  233. taskreport \"Gantt Chart\" {
  234. headline \"Project Gantt Chart\"
  235. columns hierarchindex, name, start, end, effort, duration, completed, chart
  236. timeformat \"%Y-%m-%d\"
  237. hideresource 1
  238. loadunit shortauto
  239. }
  240. resourcereport \"Resource Graph\" {
  241. headline \"Resource Allocation Graph\"
  242. columns no, name, utilization, freeload, chart
  243. loadunit shortauto
  244. sorttasks startup
  245. hidetask ~isleaf()
  246. }"
  247. :group 'org-export-taskjuggler
  248. :type '(repeat (string :tag "Report")))
  249. (defcustom org-taskjuggler-default-global-header ""
  250. "Default global header for the project.
  251. This goes before project declaration, and might be useful for
  252. early macros."
  253. :group 'org-export-taskjuggler
  254. :type '(string :tag "Preamble"))
  255. (defcustom org-taskjuggler-default-global-properties
  256. "shift s40 \"Part time shift\" {
  257. workinghours wed, thu, fri off
  258. }
  259. "
  260. "Default global properties for the project.
  261. Here you typically define global properties such as shifts,
  262. accounts, rates, vacation, macros and flags. Any property that
  263. is allowed within the TaskJuggler file can be inserted. You
  264. could for example include another TaskJuggler file.
  265. The global properties are inserted after the project declaration
  266. but before any resource and task declarations."
  267. :group 'org-export-taskjuggler
  268. :type '(string :tag "Preamble"))
  269. (defcustom org-taskjuggler-valid-task-attributes
  270. '(account start note duration endbuffer endcredit end
  271. flags journalentry length limits maxend maxstart minend
  272. minstart period reference responsible scheduling
  273. startbuffer startcredit statusnote chargeset charge)
  274. "Valid attributes for Taskjuggler tasks.
  275. If one of these appears as a property for a headline, it will be
  276. exported with the corresponding task."
  277. :group 'org-export-taskjuggler)
  278. (defcustom org-taskjuggler-valid-resource-attributes
  279. '(limits vacation shift booking efficiency journalentry rate
  280. workinghours flags)
  281. "Valid attributes for Taskjuggler resources.
  282. If one of these appears as a property for a headline, it will be
  283. exported with the corresponding resource."
  284. :group 'org-export-taskjuggler)
  285. (defcustom org-taskjuggler-valid-report-attributes
  286. '(headline columns definitions timeformat hideresource hidetask
  287. loadunit sorttasks formats period)
  288. "Valid attributes for Taskjuggler reports.
  289. If one of these appears as a property for a headline, it will be
  290. exported with the corresponding report."
  291. :group 'org-export-taskjuggler)
  292. (defcustom org-taskjuggler-keep-project-as-task t
  293. "Non-nil keeps the project headline as an umbrella task for all tasks.
  294. Setting this to nil will allow maintaining completely separated
  295. task buckets, while still sharing the same resources pool."
  296. :group 'org-export-taskjuggler
  297. :type 'boolean)
  298. ;;; Hooks
  299. (defvar org-taskjuggler-final-hook nil
  300. "Hook run after a TaskJuggler files has been saved.
  301. This hook is run with the name of the file as argument.")
  302. ;;; Back-End Definition
  303. (org-export-define-backend 'taskjuggler
  304. '((template . org-taskjuggler-project-plan))
  305. :menu-entry
  306. '(?J "Export to TaskJuggler"
  307. ((?j "As TJP file" (lambda (a s v b) (org-taskjuggler-export a s v)))
  308. (?o "As TJP file and open"
  309. (lambda (a s v b)
  310. (if a (org-taskjuggler-export a s v)
  311. (org-taskjuggler-export-and-open s v))))))
  312. ;; This property will be used to store unique ids in communication
  313. ;; channel. Ids will be retrieved with `org-taskjuggler-get-id'.
  314. :options-alist '((:taskjuggler-unique-ids nil nil nil)))
  315. ;;; Unique IDs
  316. (defun org-taskjuggler-assign-task-ids (tasks info)
  317. "Assign a unique ID to each task in TASKS.
  318. TASKS is a list of headlines. INFO is a plist used as a
  319. communication channel. Return value is an alist between
  320. headlines and their associated ID. IDs are hierarchical, which
  321. means they only need to be unique among the task siblings."
  322. (let* (alist
  323. (build-id
  324. (lambda (tasks local-ids)
  325. (org-element-map tasks 'headline
  326. (lambda (task)
  327. (let ((id (org-taskjuggler--build-unique-id task local-ids)))
  328. (push id local-ids)
  329. (push (cons task id) alist)
  330. (funcall build-id (org-element-contents task) nil)))
  331. info nil 'headline))))
  332. (funcall build-id tasks nil)
  333. alist))
  334. (defun org-taskjuggler-assign-resource-ids (resources info)
  335. "Assign a unique ID to each resource within RESOURCES.
  336. RESOURCES is a list of headlines. INFO is a plist used as a
  337. communication channel. Return value is an alist between
  338. headlines and their associated ID."
  339. (let (ids)
  340. (org-element-map resources 'headline
  341. (lambda (resource)
  342. (let ((id (org-taskjuggler--build-unique-id resource ids)))
  343. (push id ids)
  344. (cons resource id)))
  345. info)))
  346. ;;; Accessors
  347. (defun org-taskjuggler-get-project (info)
  348. "Return project in parse tree.
  349. INFO is a plist used as a communication channel. First headline
  350. in buffer with `org-taskjuggler-project-tag' defines the project.
  351. If no such task is defined, pick the first headline in buffer.
  352. If there is no headline at all, return nil."
  353. (or (org-element-map (plist-get info :parse-tree) 'headline
  354. (lambda (hl)
  355. (and (member org-taskjuggler-project-tag
  356. (org-export-get-tags hl info))
  357. hl))
  358. info t)
  359. (org-element-map tree 'headline 'identity info t)))
  360. (defun org-taskjuggler-get-id (item info)
  361. "Return id for task or resource ITEM.
  362. ITEM is a headline. INFO is a plist used as a communication
  363. channel. Return value is a string."
  364. (cdr (assq item (plist-get info :taskjuggler-unique-ids))))
  365. (defun org-taskjuggler-get-name (item)
  366. "Return name for task or resource ITEM.
  367. ITEM is a headline. Return value is a string."
  368. ;; Quote double quotes in name.
  369. (replace-regexp-in-string
  370. "\"" "\\\"" (org-element-property :raw-value item) t t))
  371. (defun org-taskjuggler-get-start (item)
  372. "Return start date for task or resource ITEM.
  373. ITEM is a headline. Return value is a string or nil if ITEM
  374. doesn't have any start date defined.."
  375. (let ((scheduled (org-element-property :scheduled item)))
  376. (and scheduled (org-timestamp-format scheduled "%Y-%02m-%02d"))))
  377. (defun org-taskjuggler-get-end (item)
  378. "Return end date for task or resource ITEM.
  379. ITEM is a headline. Return value is a string or nil if ITEM
  380. doesn't have any end date defined.."
  381. (let ((deadline (org-element-property :deadline item)))
  382. (and deadline (org-timestamp-format deadline "%Y-%02m-%02d"))))
  383. ;;; Internal Functions
  384. (defun org-taskjuggler--indent-string (s)
  385. "Indent string S by 2 spaces.
  386. Return new string. If S is the empty string, return it."
  387. (if (equal "" s) s (replace-regexp-in-string "^ *\\S-" " \\&" s)))
  388. (defun org-taskjuggler--build-attributes (item attributes)
  389. "Return attributes string for task, resource or report ITEM.
  390. ITEM is a headline. ATTRIBUTES is a list of symbols
  391. representing valid attributes for ITEM."
  392. (mapconcat
  393. (lambda (attribute)
  394. (let ((value (org-element-property
  395. (intern (upcase (format ":%s" attribute)))
  396. item)))
  397. (and value (format "%s %s\n" attribute value))))
  398. (remq nil attributes) ""))
  399. (defun org-taskjuggler--build-unique-id (item unique-ids)
  400. "Return a unique id for a given task or a resource.
  401. ITEM is an `headline' type element representing the task or
  402. resource. Its id is derived from its name and made unique
  403. against UNIQUE-IDS. If the (downcased) first token of the
  404. headline is not unique try to add more (downcased) tokens of the
  405. headline or finally add more underscore characters (\"_\")."
  406. (let ((id (org-string-nw-p (org-element-property :TASK_ID item))))
  407. ;; If an id is specified, use it, as long as it's unique.
  408. (if (and id (not (member id unique-ids))) id
  409. (let* ((parts (org-split-string (org-element-property :raw-value item)))
  410. (id (org-taskjuggler--clean-id (downcase (pop parts)))))
  411. ;; Try to add more parts of the headline to make it unique.
  412. (while (and (car parts) (member id unique-ids))
  413. (setq id (concat id "_"
  414. (org-taskjuggler--clean-id (downcase (pop parts))))))
  415. ;; If it's still not unique, add "_".
  416. (while (member id unique-ids)
  417. (setq id (concat id "_")))
  418. id))))
  419. (defun org-taskjuggler--clean-id (id)
  420. "Clean and return ID to make it acceptable for TaskJuggler.
  421. ID is a string."
  422. ;; Replace non-ascii by "_".
  423. (replace-regexp-in-string
  424. "[^a-zA-Z0-9_]" "_"
  425. ;; Make sure id doesn't start with a number.
  426. (replace-regexp-in-string "^\\([0-9]\\)" "_\\1" id)))
  427. ;;; Dependencies
  428. (defun org-taskjuggler-resolve-dependencies (task info)
  429. "Return a list of all tasks TASK depends on.
  430. TASK is a headline. INFO is a plist used as a communication
  431. channel."
  432. (let ((deps-ids
  433. ;; Get all dependencies specified in BLOCKER and DEPENDS task
  434. ;; properties. Clean options from them.
  435. (let ((deps (concat (org-element-property :BLOCKER task)
  436. (org-element-property :DEPENDS task))))
  437. (and deps
  438. (org-split-string (replace-regexp-in-string "{.*?}" "" deps)
  439. "[ ,]* +"))))
  440. depends)
  441. (when deps-ids
  442. ;; Find tasks with :task_id: property matching id in DEPS-IDS.
  443. ;; Add them to DEPENDS.
  444. (let* ((project (org-taskjuggler-get-project info))
  445. (tasks (if org-taskjuggler-keep-project-as-task project
  446. (org-element-contents project))))
  447. (setq depends
  448. (org-element-map tasks 'headline
  449. (lambda (task)
  450. (let ((task-id (org-element-property :TASK_ID task)))
  451. (and task-id (member task-id deps-ids) task)))
  452. info)))
  453. ;; Check BLOCKER and DEPENDS properties. If "previous-sibling"
  454. ;; belongs to DEPS-ID, add it to DEPENDS.
  455. (when (and (member-ignore-case "previous-sibling" deps-ids)
  456. (not (org-export-first-sibling-p task info)))
  457. (let ((prev (org-export-get-previous-element task info)))
  458. (and (not (memq prev depends)) (push prev depends)))))
  459. ;; Check ORDERED status of parent.
  460. (let ((parent (org-export-get-parent task)))
  461. (when (and parent
  462. (org-element-property :ORDERED parent)
  463. (not (org-export-first-sibling-p task info)))
  464. (push (org-export-get-previous-element task info) depends)))
  465. ;; Return dependencies.
  466. depends))
  467. (defun org-taskjuggler-format-dependencies (dependencies task info)
  468. "Format DEPENDENCIES to match TaskJuggler syntax.
  469. DEPENDENCIES is list of dependencies for TASK, as returned by
  470. `org-taskjuggler-resolve-depedencies'. TASK is a headline.
  471. INFO is a plist used as a communication channel. Return value
  472. doesn't include leading \"depends\"."
  473. (let ((dep-str (concat (org-element-property :BLOCKER task)
  474. " "
  475. (org-element-property :DEPENDS task)))
  476. (get-path
  477. (lambda (dep)
  478. ;; Return path to DEP relatively to TASK.
  479. (let ((parent (org-export-get-parent task))
  480. (exclamations 1)
  481. (option
  482. (let ((id (org-element-property :TASK_ID dep)))
  483. (and id
  484. (string-match (concat id " +\\({.*?}\\)") dep-str)
  485. (org-match-string-no-properties 1))))
  486. path)
  487. ;; Compute number of exclamation marks by looking for the
  488. ;; common ancestor between TASK and DEP.
  489. (while (not (org-element-map parent 'headline
  490. (lambda (hl) (eq hl dep))))
  491. (incf exclamations)
  492. (setq parent (org-export-get-parent parent)))
  493. ;; Build path from DEP to PARENT.
  494. (while (not (eq parent dep))
  495. (push (org-taskjuggler-get-id dep info) path)
  496. (setq dep (org-export-get-parent dep)))
  497. ;; Return full path. Add dependency options, if any.
  498. (concat (make-string exclamations ?!)
  499. (mapconcat 'identity path ".")
  500. (and option (concat " " option)))))))
  501. ;; Return dependencies string, without the leading "depends".
  502. (mapconcat (lambda (dep) (funcall get-path dep)) dependencies ", ")))
  503. ;;; Translator Functions
  504. (defun org-taskjuggler-project-plan (contents info)
  505. "Build TaskJuggler project plan.
  506. CONTENTS is ignored. INFO is a plist holding export options.
  507. Return complete project plan as a string in TaskJuggler syntax."
  508. (let* ((tree (plist-get info :parse-tree))
  509. (project (or (org-taskjuggler-get-project info)
  510. (error "No project specified"))))
  511. (concat
  512. ;; 1. Insert header.
  513. (org-element-normalize-string org-taskjuggler-default-global-header)
  514. ;; 2. Insert project.
  515. (org-taskjuggler--build-project project info)
  516. ;; 3. Insert global properties.
  517. (org-element-normalize-string org-taskjuggler-default-global-properties)
  518. ;; 4. Insert resources. Provide a default one if none is
  519. ;; specified.
  520. (let ((main-resources
  521. ;; Collect contents from various trees marked with
  522. ;; `org-taskjuggler-resource-tag'. Only gather top level
  523. ;; resources.
  524. (apply 'append
  525. (org-element-map tree 'headline
  526. (lambda (hl)
  527. (and (member org-taskjuggler-resource-tag
  528. (org-export-get-tags hl info))
  529. (org-element-map (org-element-contents hl) 'headline
  530. 'identity info nil 'headline)))
  531. info nil 'headline))))
  532. ;; Assign a unique ID to each resource. Store it under
  533. ;; `:taskjuggler-unique-ids' property in INFO.
  534. (setq info
  535. (plist-put info :taskjuggler-unique-ids
  536. (org-taskjuggler-assign-resource-ids
  537. main-resources info)))
  538. (concat
  539. (if main-resources
  540. (mapconcat
  541. (lambda (resource) (org-taskjuggler--build-resource resource info))
  542. main-resources "")
  543. (format "resource %s \"%s\" {\n}\n" (user-login-name) user-full-name))
  544. ;; 5. Insert tasks.
  545. (let ((main-tasks
  546. ;; If `org-taskjuggler-keep-project-as-task' is
  547. ;; non-nil, there is only one task. Otherwise, every
  548. ;; direct children of PROJECT is a top level task.
  549. (if org-taskjuggler-keep-project-as-task (list project)
  550. (or (org-element-map (org-element-contents project) 'headline
  551. 'identity info nil 'headline)
  552. (error "No task specified")))))
  553. ;; Assign a unique ID to each task. Add it to
  554. ;; `:taskjuggler-unique-ids' property in INFO.
  555. (setq info
  556. (plist-put info :taskjuggler-unique-ids
  557. (append
  558. (org-taskjuggler-assign-task-ids main-tasks info)
  559. (plist-get info :taskjuggler-unique-ids))))
  560. ;; If no resource is allocated among tasks, allocate one to
  561. ;; the first task.
  562. (unless (org-element-map main-tasks 'headline
  563. (lambda (task) (org-element-property :ALLOCATE task))
  564. info t)
  565. (org-element-put-property
  566. (car main-tasks) :ALLOCATE
  567. (or (org-taskjuggler-get-id (car main-resources) info)
  568. (user-login-name))))
  569. (mapconcat
  570. (lambda (task) (org-taskjuggler--build-task task info))
  571. main-tasks ""))
  572. ;; 6. Insert reports. If no report is defined, insert default
  573. ;; reports.
  574. (let ((main-reports
  575. ;; Collect contents from various trees marked with
  576. ;; `org-taskjuggler-report-tag'. Only gather top level
  577. ;; reports.
  578. (apply 'append
  579. (org-element-map tree 'headline
  580. (lambda (hl)
  581. (and (member org-taskjuggler-report-tag
  582. (org-export-get-tags hl info))
  583. (org-element-map (org-element-contents hl)
  584. 'headline 'identity info nil 'headline)))
  585. info nil 'headline))))
  586. (if main-reports
  587. (mapconcat
  588. (lambda (report) (org-taskjuggler--build-report report info))
  589. main-reports "")
  590. (mapconcat 'org-element-normalize-string
  591. org-taskjuggler-default-reports ""))))))))
  592. (defun org-taskjuggler--build-project (project info)
  593. "Return a project declaration.
  594. PROJECT is a headline. INFO is a plist used as a communication
  595. channel. If no start date is specified, start today. If no end
  596. date is specified, end `org-taskjuggler-default-project-duration'
  597. days from now."
  598. (format "project %s \"%s\" \"%s\" %s %s {\n}\n"
  599. (org-taskjuggler-get-id project info)
  600. (org-taskjuggler-get-name project)
  601. ;; Version is obtained through :TASKJUGGLER_VERSION:
  602. ;; property or `org-taskjuggler-default-project-version'.
  603. (or (org-element-property :VERSION project)
  604. org-taskjuggler-default-project-version)
  605. (or (org-taskjuggler-get-start project)
  606. (format-time-string "%Y-%m-%d"))
  607. (let ((end (org-taskjuggler-get-end project)))
  608. (or (and end (format "- %s" end))
  609. (format "+%sd" org-taskjuggler-default-project-duration)))))
  610. (defun org-taskjuggler--build-resource (resource info)
  611. "Return a resource declaration.
  612. RESOURCE is a headline. INFO is a plist used as a communication
  613. channel.
  614. All valid attributes from RESOURCE are inserted. If RESOURCE
  615. defines a property \"resource_id\" it will be used as the id for
  616. this resource. Otherwise it will use the ID property. If
  617. neither is defined a unique id will be associated to it."
  618. (concat
  619. ;; Opening resource.
  620. (format "resource %s \"%s\" {\n"
  621. (org-taskjuggler--clean-id
  622. (or (org-element-property :RESOURCE_ID resource)
  623. (org-element-property :ID resource)
  624. (org-taskjuggler-get-id resource info)))
  625. (org-taskjuggler-get-name resource))
  626. ;; Add attributes.
  627. (org-taskjuggler--indent-string
  628. (org-taskjuggler--build-attributes
  629. resource org-taskjuggler-valid-resource-attributes))
  630. ;; Add inner resources.
  631. (org-taskjuggler--indent-string
  632. (mapconcat
  633. 'identity
  634. (org-element-map (org-element-contents resource) 'headline
  635. (lambda (hl) (org-taskjuggler--build-resource hl info))
  636. info nil 'headline)
  637. ""))
  638. ;; Closing resource.
  639. "}\n"))
  640. (defun org-taskjuggler--build-report (report info)
  641. "Return a report declaration.
  642. REPORT is a headline. INFO is a plist used as a communication
  643. channel."
  644. (concat
  645. ;; Opening report.
  646. (format "%s \"%s\" {\n"
  647. (or (org-element-property :REPORT_KIND report) "taskreport")
  648. (org-taskjuggler-get-name report))
  649. ;; Add attributes.
  650. (org-taskjuggler--indent-string
  651. (org-taskjuggler--build-attributes
  652. report org-taskjuggler-valid-report-attributes))
  653. ;; Add inner reports.
  654. (org-taskjuggler--indent-string
  655. (mapconcat
  656. 'identity
  657. (org-element-map (org-element-contents report) 'headline
  658. (lambda (hl) (org-taskjuggler--build-report hl info))
  659. info nil 'headline)
  660. ""))
  661. ;; Closing report.
  662. "}\n"))
  663. (defun org-taskjuggler--build-task (task info)
  664. "Return a task declaration.
  665. TASK is a headline. INFO is a plist used as a communication
  666. channel.
  667. All valid attributes from TASK are inserted. If TASK defines
  668. a property \"task_id\" it will be used as the id for this task.
  669. Otherwise it will use the ID property. If neither is defined
  670. a unique id will be associated to it."
  671. (let* ((allocate (org-element-property :ALLOCATE task))
  672. (complete
  673. (if (eq (org-element-property :todo-type task) 'done) "100"
  674. (org-element-property :COMPLETE task)))
  675. (depends (org-taskjuggler-resolve-dependencies task info))
  676. (effort (org-element-property :EFFORT task))
  677. (milestone
  678. (or (org-element-property :MILESTONE task)
  679. (not (or (org-element-map (org-element-contents task) 'headline
  680. 'identity info t) ; Has task any child?
  681. effort
  682. (org-element-property :LENGTH task)
  683. (org-element-property :DURATION task)
  684. (and (org-taskjuggler-get-start task)
  685. (org-taskjuggler-get-end task))
  686. (org-element-property :PERIOD task)))))
  687. (priority
  688. (let ((pri (org-element-property :priority task)))
  689. (and pri
  690. (max 1 (/ (* 1000 (- org-lowest-priority pri))
  691. (- org-lowest-priority org-highest-priority)))))))
  692. (concat
  693. ;; Opening task.
  694. (format "task %s \"%s\" {\n"
  695. (org-taskjuggler-get-id task info)
  696. (org-taskjuggler-get-name task))
  697. ;; Add default attributes.
  698. (and depends
  699. (format " depends %s\n"
  700. (org-taskjuggler-format-dependencies depends task info)))
  701. (and allocate
  702. (format " purge %s\n allocate %s\n"
  703. ;; Compatibility for previous TaskJuggler versions.
  704. (if (>= org-taskjuggler-target-version 3.0) "allocate"
  705. "allocations")
  706. allocate))
  707. (and complete (format " complete %s\n" complete))
  708. (and effort
  709. (format " effort %s\n"
  710. (let* ((minutes (org-duration-string-to-minutes effort))
  711. (hours (/ minutes 60.0)))
  712. (format "%.1fh" hours))))
  713. (and priority (format " priority %s\n" complete))
  714. (and milestone " milestone\n")
  715. ;; Add other valid attributes.
  716. (org-taskjuggler--indent-string
  717. (org-taskjuggler--build-attributes
  718. task org-taskjuggler-valid-task-attributes))
  719. ;; Add inner tasks.
  720. (org-taskjuggler--indent-string
  721. (mapconcat 'identity
  722. (org-element-map (org-element-contents task) 'headline
  723. (lambda (hl) (org-taskjuggler--build-task hl info))
  724. info nil 'headline)
  725. ""))
  726. ;; Closing task.
  727. "}\n")))
  728. ;;; Interactive Functions
  729. ;;;###autoload
  730. (defun org-taskjuggler-export (&optional async subtreep visible-only)
  731. "Export current buffer to a TaskJuggler file.
  732. The exporter looks for a tree with tag that matches
  733. `org-taskjuggler-project-tag' and takes this as the tasks for
  734. this project. The first node of this tree defines the project
  735. properties such as project name and project period.
  736. If there is a tree with tag that matches
  737. `org-taskjuggler-resource-tag' this tree is taken as resources
  738. for the project. If no resources are specified, a default
  739. resource is created and allocated to the project.
  740. Also the TaskJuggler project will be created with default reports
  741. as defined in `org-taskjuggler-default-reports'.
  742. If narrowing is active in the current buffer, only export its
  743. narrowed part.
  744. If a region is active, export that region.
  745. A non-nil optional argument ASYNC means the process should happen
  746. asynchronously. The resulting file should be accessible through
  747. the `org-export-stack' interface.
  748. When optional argument SUBTREEP is non-nil, export the sub-tree
  749. at point, extracting information from the headline properties
  750. first.
  751. When optional argument VISIBLE-ONLY is non-nil, don't export
  752. contents of hidden elements.
  753. Return output file's name."
  754. (interactive)
  755. (let ((outfile
  756. (org-export-output-file-name org-taskjuggler-extension subtreep)))
  757. (if async
  758. (org-export-async-start
  759. (lambda (f)
  760. (org-export-add-to-stack f 'taskjuggler)
  761. (run-hook-with-args 'org-taskjuggler-final-hook f))
  762. `(expand-file-name
  763. (org-export-to-file 'taskjuggler ,outfile ,subtreep ,visible-only)))
  764. (org-export-to-file 'taskjuggler outfile subtreep visible-only)
  765. (run-hook-with-args 'org-taskjuggler-final-hook outfile)
  766. outfile)))
  767. ;;;###autoload
  768. (defun org-taskjuggler-export-and-open (&optional subtreep visible-only)
  769. "Export current buffer to a TaskJuggler file and open it.
  770. The exporter looks for a tree with tag that matches
  771. `org-taskjuggler-project-tag' and takes this as the tasks for
  772. this project. The first node of this tree defines the project
  773. properties such as project name and project period.
  774. If there is a tree with tag that matches
  775. `org-taskjuggler-resource-tag' this tree is taken as resources
  776. for the project. If no resources are specified, a default
  777. resource is created and allocated to the project.
  778. Also the TaskJuggler project will be created with default reports
  779. as defined in `org-taskjuggler-default-reports'.
  780. If narrowing is active in the current buffer, only export its
  781. narrowed part.
  782. If a region is active, export that region.
  783. When optional argument SUBTREEP is non-nil, export the sub-tree
  784. at point, extracting information from the headline properties
  785. first.
  786. When optional argument VISIBLE-ONLY is non-nil, don't export
  787. contents of hidden elements.
  788. Open file with the TaskJuggler GUI."
  789. (interactive)
  790. (let* ((file (org-taskjuggler-export nil subtreep visible-only))
  791. (process-name "TaskJugglerUI")
  792. (command (concat process-name " " file)))
  793. (start-process-shell-command process-name nil command)))
  794. (provide 'ox-taskjuggler)
  795. ;; Local variables:
  796. ;; sentence-end-double-space: t
  797. ;; End:
  798. ;;; ox-taskjuggler.el ends here