ox-taskjuggler.el 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  1. ;;; ox-taskjuggler.el --- TaskJuggler Back-End for Org Export Engine
  2. ;;
  3. ;; Copyright (C) 2007-2014 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. ;; - Add support for org-export-with-planning
  140. ;;
  141. ;;; Code:
  142. (eval-when-compile (require 'cl))
  143. (require 'ox)
  144. ;;; User Variables
  145. (defgroup org-export-taskjuggler nil
  146. "Options specific for TaskJuggler export back-end."
  147. :tag "Org Export TaskJuggler"
  148. :group 'org-export)
  149. (defcustom org-taskjuggler-extension ".tjp"
  150. "Extension of TaskJuggler files."
  151. :group 'org-export-taskjuggler
  152. :type 'string)
  153. (defcustom org-taskjuggler-project-tag "taskjuggler_project"
  154. "Tag marking project's tasks.
  155. This tag is used to find the tree containing all the tasks for
  156. the project."
  157. :group 'org-export-taskjuggler
  158. :type 'string)
  159. (defcustom org-taskjuggler-resource-tag "taskjuggler_resource"
  160. "Tag marking project's resources.
  161. This tag is used to find the tree containing all the resources
  162. for the project."
  163. :group 'org-export-taskjuggler
  164. :type 'string)
  165. (defcustom org-taskjuggler-report-tag "taskjuggler_report"
  166. "Tag marking project's reports.
  167. This tag is used to find the tree containing all the reports for
  168. the project."
  169. :group 'org-export-taskjuggler
  170. :type 'string)
  171. (defcustom org-taskjuggler-target-version 3.0
  172. "Which version of TaskJuggler the exporter is targeting.
  173. By default a project plan is exported which conforms to version
  174. 3.x of TaskJuggler. For a project plan that is compatible with
  175. versions of TaskJuggler older than 3.0 set this to 2.4.
  176. If you change this variable be sure to also change
  177. `org-taskjuggler-default-reports' as the format of reports has
  178. changed considerably between version 2.x and 3.x of TaskJuggler"
  179. :group 'org-export-taskjuggler
  180. :type 'number)
  181. (defcustom org-taskjuggler-default-project-version "1.0"
  182. "Default version string for the project.
  183. This value can also be set with the \":VERSION:\" property
  184. associated to the headline defining the project."
  185. :group 'org-export-taskjuggler
  186. :type 'string)
  187. (defcustom org-taskjuggler-default-project-duration 280
  188. "Default project duration.
  189. The value will be used if no start and end date have been defined
  190. in the root node of the task tree, i.e. the tree that has been
  191. marked with `org-taskjuggler-project-tag'"
  192. :group 'org-export-taskjuggler
  193. :type 'integer)
  194. (defcustom org-taskjuggler-default-reports
  195. '("textreport report \"Plan\" {
  196. formats html
  197. header '== %title =='
  198. center -8<-
  199. [#Plan Plan] | [#Resource_Allocation Resource Allocation]
  200. ----
  201. === Plan ===
  202. <[report id=\"plan\"]>
  203. ----
  204. === Resource Allocation ===
  205. <[report id=\"resourceGraph\"]>
  206. ->8-
  207. }
  208. # A traditional Gantt chart with a project overview.
  209. taskreport plan \"\" {
  210. headline \"Project Plan\"
  211. columns bsi, name, start, end, effort, chart
  212. loadunit shortauto
  213. hideresource 1
  214. }
  215. # A graph showing resource allocation. It identifies whether each
  216. # resource is under- or over-allocated for.
  217. resourcereport resourceGraph \"\" {
  218. headline \"Resource Allocation Graph\"
  219. columns no, name, effort, weekly
  220. loadunit shortauto
  221. hidetask ~(isleaf() & isleaf_())
  222. sorttasks plan.start.up
  223. }")
  224. "Default reports for the project.
  225. These are sensible default reports to give a good out-of-the-box
  226. result when exporting without defining any reports. \"%title\"
  227. anywhere in the reports will be replaced with the document title.
  228. If you want to define your own reports you can change them here
  229. or simply define the default reports so that they include an
  230. external report definition as follows:
  231. include reports.tji
  232. These default are made to work with tj3. If you are targeting
  233. TaskJuggler 2.4 (see `org-taskjuggler-target-version') please
  234. change these defaults to something like the following:
  235. taskreport \"Gantt Chart\" {
  236. headline \"Project Gantt Chart\"
  237. columns hierarchindex, name, start, end, effort, duration, completed, chart
  238. timeformat \"%Y-%m-%d\"
  239. hideresource 1
  240. loadunit shortauto
  241. }
  242. resourcereport \"Resource Graph\" {
  243. headline \"Resource Allocation Graph\"
  244. columns no, name, utilization, freeload, chart
  245. loadunit shortauto
  246. sorttasks startup
  247. hidetask ~isleaf()
  248. }"
  249. :group 'org-export-taskjuggler
  250. :type '(repeat (string :tag "Report")))
  251. (defcustom org-taskjuggler-default-global-header ""
  252. "Default global header for the project.
  253. This goes before project declaration, and might be useful for
  254. early macros."
  255. :group 'org-export-taskjuggler
  256. :type '(string :tag "Preamble"))
  257. (defcustom org-taskjuggler-default-global-properties
  258. "shift s40 \"Part time shift\" {
  259. workinghours wed, thu, fri off
  260. }
  261. "
  262. "Default global properties for the project.
  263. Here you typically define global properties such as shifts,
  264. accounts, rates, vacation, macros and flags. Any property that
  265. is allowed within the TaskJuggler file can be inserted. You
  266. could for example include another TaskJuggler file.
  267. The global properties are inserted after the project declaration
  268. but before any resource and task declarations."
  269. :group 'org-export-taskjuggler
  270. :type '(string :tag "Preamble"))
  271. (defcustom org-taskjuggler-valid-task-attributes
  272. '(account start note duration endbuffer endcredit end
  273. flags journalentry length limits maxend maxstart minend
  274. minstart period reference responsible scheduling
  275. startbuffer startcredit statusnote chargeset charge)
  276. "Valid attributes for Taskjuggler tasks.
  277. If one of these appears as a property for a headline, it will be
  278. exported with the corresponding task."
  279. :group 'org-export-taskjuggler)
  280. (defcustom org-taskjuggler-valid-project-attributes
  281. '(timingresolution timezone alertlevels currency currencyformat
  282. dailyworkinghours extend includejournalentry now numberformat
  283. outputdir scenario shorttimeformat timeformat trackingscenario
  284. weekstartsmonday weekstartssunday workinghours
  285. yearlyworkingdays)
  286. "Valid attributes for Taskjuggler project.
  287. If one of these appears as a property for a headline that is a
  288. project definition, it will be exported with the corresponding
  289. task. Attribute 'timingresolution' should be the first in the
  290. list."
  291. :group 'org-export-taskjuggler)
  292. (defcustom org-taskjuggler-valid-resource-attributes
  293. '(limits vacation shift booking efficiency journalentry rate
  294. workinghours flags)
  295. "Valid attributes for Taskjuggler resources.
  296. If one of these appears as a property for a headline, it will be
  297. exported with the corresponding resource."
  298. :group 'org-export-taskjuggler)
  299. (defcustom org-taskjuggler-valid-report-attributes
  300. '(headline columns definitions timeformat hideresource hidetask
  301. loadunit sorttasks formats period)
  302. "Valid attributes for Taskjuggler reports.
  303. If one of these appears as a property for a headline, it will be
  304. exported with the corresponding report."
  305. :group 'org-export-taskjuggler)
  306. (defcustom org-taskjuggler-process-command
  307. "tj3 --silent --no-color --output-dir %o %f"
  308. "Command to process a Taskjuggler file.
  309. The command will be given to the shell as a command to process a
  310. Taskjuggler file. \"%f\" in the command will be replaced by the
  311. full file name, \"%o\" by the reports directory (see
  312. `org-taskjuggler-reports-directory').
  313. If you are targeting Taskjuggler 2.4 (see
  314. `org-taskjuggler-target-version') this setting is ignored."
  315. :group 'org-export-taskjuggler)
  316. (defcustom org-taskjuggler-reports-directory "reports"
  317. "Default directory to generate the Taskjuggler reports in.
  318. The command `org-taskjuggler-process-command' generates the
  319. reports and associated files such as CSS inside this directory.
  320. If the directory is not an absolute path it is relative to the
  321. directory of the exported file. The directory is created if it
  322. doesn't exist.
  323. If you are targeting Taskjuggler 2.4 (see
  324. `org-taskjuggler-target-version') this setting is ignored."
  325. :group 'org-export-taskjuggler)
  326. (defcustom org-taskjuggler-keep-project-as-task t
  327. "Non-nil keeps the project headline as an umbrella task for all tasks.
  328. Setting this to nil will allow maintaining completely separated
  329. task buckets, while still sharing the same resources pool."
  330. :group 'org-export-taskjuggler
  331. :type 'boolean)
  332. ;;; Hooks
  333. (defvar org-taskjuggler-final-hook nil
  334. "Hook run after a TaskJuggler files has been saved.
  335. This hook is run with the name of the file as argument.")
  336. ;;; Back-End Definition
  337. (org-export-define-backend 'taskjuggler
  338. '((template . org-taskjuggler-project-plan))
  339. :menu-entry
  340. '(?J "Export to TaskJuggler"
  341. ((?j "As TJP file" (lambda (a s v b) (org-taskjuggler-export a s v)))
  342. (?p "As TJP file and process"
  343. (lambda (a s v b)
  344. (if a (org-taskjuggler-export a s v)
  345. (org-taskjuggler-export-and-process s v))))
  346. (?o "As TJP file, process and open"
  347. (lambda (a s v b)
  348. (if a (org-taskjuggler-export a s v)
  349. (org-taskjuggler-export-process-and-open s v))))))
  350. ;; This property will be used to store unique ids in communication
  351. ;; channel. Ids will be retrieved with `org-taskjuggler-get-id'.
  352. :options-alist '((:taskjuggler-unique-ids nil nil nil)))
  353. ;;; Unique IDs
  354. (defun org-taskjuggler-assign-task-ids (tasks info)
  355. "Assign a unique ID to each task in TASKS.
  356. TASKS is a list of headlines. INFO is a plist used as a
  357. communication channel. Return value is an alist between
  358. headlines and their associated ID. IDs are hierarchical, which
  359. means they only need to be unique among the task siblings."
  360. (let* (alist
  361. build-id ; For byte-compiler.
  362. (build-id
  363. (lambda (tasks local-ids)
  364. (org-element-map tasks 'headline
  365. (lambda (task)
  366. (let ((id (org-taskjuggler--build-unique-id task local-ids)))
  367. (push id local-ids)
  368. (push (cons task id) alist)
  369. (funcall build-id (org-element-contents task) nil)))
  370. info nil 'headline))))
  371. (funcall build-id tasks nil)
  372. alist))
  373. (defun org-taskjuggler-assign-resource-ids (resources info)
  374. "Assign a unique ID to each resource within RESOURCES.
  375. RESOURCES is a list of headlines. INFO is a plist used as a
  376. communication channel. Return value is an alist between
  377. headlines and their associated ID."
  378. (let (ids)
  379. (org-element-map resources 'headline
  380. (lambda (resource)
  381. (let ((id (org-taskjuggler--build-unique-id resource ids)))
  382. (push id ids)
  383. (cons resource id)))
  384. info)))
  385. ;;; Accessors
  386. (defun org-taskjuggler-get-project (info)
  387. "Return project in parse tree.
  388. INFO is a plist used as a communication channel. First headline
  389. in buffer with `org-taskjuggler-project-tag' defines the project.
  390. If no such task is defined, pick the first headline in buffer.
  391. If there is no headline at all, return nil."
  392. (let ((tree (plist-get info :parse-tree)))
  393. (or (org-element-map tree 'headline
  394. (lambda (hl)
  395. (and (member org-taskjuggler-project-tag
  396. (org-export-get-tags hl info))
  397. hl))
  398. info t)
  399. (org-element-map tree 'headline 'identity info t))))
  400. (defun org-taskjuggler-get-id (item info)
  401. "Return id for task or resource ITEM.
  402. ITEM is a headline. INFO is a plist used as a communication
  403. channel. Return value is a string."
  404. (cdr (assq item (plist-get info :taskjuggler-unique-ids))))
  405. (defun org-taskjuggler-get-name (item)
  406. "Return name for task or resource ITEM.
  407. ITEM is a headline. Return value is a string."
  408. ;; Quote double quotes in name.
  409. (replace-regexp-in-string
  410. "\"" "\\\"" (org-element-property :raw-value item) t t))
  411. (defun org-taskjuggler-get-start (item)
  412. "Return start date for task or resource ITEM.
  413. ITEM is a headline. Return value is a string or nil if ITEM
  414. doesn't have any start date defined."
  415. (let ((scheduled (org-element-property :scheduled item)))
  416. (or
  417. (and scheduled (org-timestamp-format scheduled "%Y-%02m-%02d"))
  418. (and (memq 'start org-taskjuggler-valid-task-attributes)
  419. (org-element-property :START item)))))
  420. (defun org-taskjuggler-get-end (item)
  421. "Return end date for task or resource ITEM.
  422. ITEM is a headline. Return value is a string or nil if ITEM
  423. doesn't have any end date defined."
  424. (let ((deadline (org-element-property :deadline item)))
  425. (and deadline (org-timestamp-format deadline "%Y-%02m-%02d"))))
  426. ;;; Internal Functions
  427. (defun org-taskjuggler--indent-string (s)
  428. "Indent string S by 2 spaces.
  429. Return new string. If S is the empty string, return it."
  430. (if (equal "" s) s (replace-regexp-in-string "^ *\\S-" " \\&" s)))
  431. (defun org-taskjuggler--build-attributes (item attributes)
  432. "Return attributes string for ITEM.
  433. ITEM is a project, task, resource or report headline. ATTRIBUTES
  434. is a list of symbols representing valid attributes for ITEM."
  435. (mapconcat
  436. (lambda (attribute)
  437. (let ((value (org-element-property
  438. (intern (upcase (format ":%s" attribute)))
  439. item)))
  440. (and value (format "%s %s\n" attribute value))))
  441. (remq nil attributes) ""))
  442. (defun org-taskjuggler--build-unique-id (item unique-ids)
  443. "Return a unique id for a given task or a resource.
  444. ITEM is an `headline' type element representing the task or
  445. resource. Its id is derived from its name and made unique
  446. against UNIQUE-IDS. If the (downcased) first token of the
  447. headline is not unique try to add more (downcased) tokens of the
  448. headline or finally add more underscore characters (\"_\")."
  449. (let ((id (org-string-nw-p (org-element-property :TASK_ID item))))
  450. ;; If an id is specified, use it, as long as it's unique.
  451. (if (and id (not (member id unique-ids))) id
  452. (let* ((parts (org-split-string (org-element-property :raw-value item)))
  453. (id (org-taskjuggler--clean-id (downcase (pop parts)))))
  454. ;; Try to add more parts of the headline to make it unique.
  455. (while (and (car parts) (member id unique-ids))
  456. (setq id (concat id "_"
  457. (org-taskjuggler--clean-id (downcase (pop parts))))))
  458. ;; If it's still not unique, add "_".
  459. (while (member id unique-ids)
  460. (setq id (concat id "_")))
  461. id))))
  462. (defun org-taskjuggler--clean-id (id)
  463. "Clean and return ID to make it acceptable for TaskJuggler.
  464. ID is a string."
  465. ;; Replace non-ascii by "_".
  466. (replace-regexp-in-string
  467. "[^a-zA-Z0-9_]" "_"
  468. ;; Make sure id doesn't start with a number.
  469. (replace-regexp-in-string "^\\([0-9]\\)" "_\\1" id)))
  470. ;;; Dependencies
  471. (defun org-taskjuggler-resolve-dependencies (task info)
  472. "Return a list of all tasks TASK depends on.
  473. TASK is a headline. INFO is a plist used as a communication
  474. channel."
  475. (let ((deps-ids
  476. ;; Get all dependencies specified in BLOCKER and DEPENDS task
  477. ;; properties. Clean options from them.
  478. (let ((deps (concat (org-element-property :BLOCKER task)
  479. (org-element-property :DEPENDS task))))
  480. (and deps
  481. (org-split-string (replace-regexp-in-string "{.*?}" "" deps)
  482. "[ ,]* +"))))
  483. depends)
  484. (when deps-ids
  485. ;; Find tasks with :task_id: property matching id in DEPS-IDS.
  486. ;; Add them to DEPENDS.
  487. (let* ((project (org-taskjuggler-get-project info))
  488. (tasks (if org-taskjuggler-keep-project-as-task project
  489. (org-element-contents project))))
  490. (setq depends
  491. (org-element-map tasks 'headline
  492. (lambda (task)
  493. (let ((task-id (or (org-element-property :TASK_ID task)
  494. (org-element-property :ID task))))
  495. (and task-id (member task-id deps-ids) task)))
  496. info)))
  497. ;; Check BLOCKER and DEPENDS properties. If "previous-sibling"
  498. ;; belongs to DEPS-ID, add it to DEPENDS.
  499. (when (and (member-ignore-case "previous-sibling" deps-ids)
  500. (not (org-export-first-sibling-p task info)))
  501. (let ((prev (org-export-get-previous-element task info)))
  502. (and (not (memq prev depends)) (push prev depends)))))
  503. ;; Check ORDERED status of parent.
  504. (let ((parent (org-export-get-parent task)))
  505. (when (and parent
  506. (org-element-property :ORDERED parent)
  507. (not (org-export-first-sibling-p task info)))
  508. (push (org-export-get-previous-element task info) depends)))
  509. ;; Return dependencies.
  510. depends))
  511. (defun org-taskjuggler-format-dependencies (dependencies task info)
  512. "Format DEPENDENCIES to match TaskJuggler syntax.
  513. DEPENDENCIES is list of dependencies for TASK, as returned by
  514. `org-taskjuggler-resolve-depedencies'. TASK is a headline.
  515. INFO is a plist used as a communication channel. Return value
  516. doesn't include leading \"depends\"."
  517. (let* ((dep-str (concat (org-element-property :BLOCKER task)
  518. " "
  519. (org-element-property :DEPENDS task)))
  520. (get-path
  521. (lambda (dep)
  522. ;; Return path to DEP relatively to TASK.
  523. (let ((parent (org-export-get-parent task))
  524. (exclamations 1)
  525. (option
  526. (let ((id (org-element-property :TASK_ID dep)))
  527. (and id
  528. (string-match (concat id " +\\({.*?}\\)") dep-str)
  529. (org-match-string-no-properties 1))))
  530. path)
  531. ;; Compute number of exclamation marks by looking for the
  532. ;; common ancestor between TASK and DEP.
  533. (while (not (org-element-map parent 'headline
  534. (lambda (hl) (eq hl dep))))
  535. (incf exclamations)
  536. (setq parent (org-export-get-parent parent)))
  537. ;; Build path from DEP to PARENT.
  538. (while (not (eq parent dep))
  539. (push (org-taskjuggler-get-id dep info) path)
  540. (setq dep (org-export-get-parent dep)))
  541. ;; Return full path. Add dependency options, if any.
  542. (concat (make-string exclamations ?!)
  543. (mapconcat 'identity path ".")
  544. (and option (concat " " option)))))))
  545. ;; Return dependencies string, without the leading "depends".
  546. (mapconcat (lambda (dep) (funcall get-path dep)) dependencies ", ")))
  547. ;;; Translator Functions
  548. (defun org-taskjuggler-project-plan (contents info)
  549. "Build TaskJuggler project plan.
  550. CONTENTS is ignored. INFO is a plist holding export options.
  551. Return complete project plan as a string in TaskJuggler syntax."
  552. (let* ((tree (plist-get info :parse-tree))
  553. (project (or (org-taskjuggler-get-project info)
  554. (error "No project specified"))))
  555. (concat
  556. ;; 1. Insert header.
  557. (org-element-normalize-string org-taskjuggler-default-global-header)
  558. ;; 2. Insert project.
  559. (org-taskjuggler--build-project project info)
  560. ;; 3. Insert global properties.
  561. (org-element-normalize-string org-taskjuggler-default-global-properties)
  562. ;; 4. Insert resources. Provide a default one if none is
  563. ;; specified.
  564. (let ((main-resources
  565. ;; Collect contents from various trees marked with
  566. ;; `org-taskjuggler-resource-tag'. Only gather top level
  567. ;; resources.
  568. (apply 'append
  569. (org-element-map tree 'headline
  570. (lambda (hl)
  571. (and (member org-taskjuggler-resource-tag
  572. (org-export-get-tags hl info))
  573. (org-element-map (org-element-contents hl) 'headline
  574. 'identity info nil 'headline)))
  575. info nil 'headline))))
  576. ;; Assign a unique ID to each resource. Store it under
  577. ;; `:taskjuggler-unique-ids' property in INFO.
  578. (setq info
  579. (plist-put info :taskjuggler-unique-ids
  580. (org-taskjuggler-assign-resource-ids
  581. main-resources info)))
  582. (concat
  583. (if main-resources
  584. (mapconcat
  585. (lambda (resource) (org-taskjuggler--build-resource resource info))
  586. main-resources "")
  587. (format "resource %s \"%s\" {\n}\n" (user-login-name) user-full-name))
  588. ;; 5. Insert tasks.
  589. (let ((main-tasks
  590. ;; If `org-taskjuggler-keep-project-as-task' is
  591. ;; non-nil, there is only one task. Otherwise, every
  592. ;; direct children of PROJECT is a top level task.
  593. (if org-taskjuggler-keep-project-as-task (list project)
  594. (or (org-element-map (org-element-contents project) 'headline
  595. 'identity info nil 'headline)
  596. (error "No task specified")))))
  597. ;; Assign a unique ID to each task. Add it to
  598. ;; `:taskjuggler-unique-ids' property in INFO.
  599. (setq info
  600. (plist-put info :taskjuggler-unique-ids
  601. (append
  602. (org-taskjuggler-assign-task-ids main-tasks info)
  603. (plist-get info :taskjuggler-unique-ids))))
  604. ;; If no resource is allocated among tasks, allocate one to
  605. ;; the first task.
  606. (unless (org-element-map main-tasks 'headline
  607. (lambda (task) (org-element-property :ALLOCATE task))
  608. info t)
  609. (org-element-put-property
  610. (car main-tasks) :ALLOCATE
  611. (or (org-taskjuggler-get-id (car main-resources) info)
  612. (user-login-name))))
  613. (mapconcat
  614. (lambda (task) (org-taskjuggler--build-task task info))
  615. main-tasks ""))
  616. ;; 6. Insert reports. If no report is defined, insert default
  617. ;; reports.
  618. (let ((main-reports
  619. ;; Collect contents from various trees marked with
  620. ;; `org-taskjuggler-report-tag'. Only gather top level
  621. ;; reports.
  622. (apply 'append
  623. (org-element-map tree 'headline
  624. (lambda (hl)
  625. (and (member org-taskjuggler-report-tag
  626. (org-export-get-tags hl info))
  627. (org-element-map (org-element-contents hl)
  628. 'headline 'identity info nil 'headline)))
  629. info nil 'headline))))
  630. (if main-reports
  631. (mapconcat
  632. (lambda (report) (org-taskjuggler--build-report report info))
  633. main-reports "")
  634. ;; insert title in default reports
  635. (let* ((title (org-export-data (plist-get info :title) info))
  636. (report-title (if (string= title "")
  637. (org-taskjuggler-get-name project)
  638. title)))
  639. (mapconcat
  640. 'org-element-normalize-string
  641. (mapcar
  642. (function
  643. (lambda (report)
  644. (replace-regexp-in-string "%title" report-title report t t)))
  645. org-taskjuggler-default-reports) "")))))))))
  646. (defun org-taskjuggler--build-project (project info)
  647. "Return a project declaration.
  648. PROJECT is a headline. INFO is a plist used as a communication
  649. channel. If no start date is specified, start today. If no end
  650. date is specified, end `org-taskjuggler-default-project-duration'
  651. days from now."
  652. (concat
  653. ;; Opening project.
  654. (format "project %s \"%s\" \"%s\" %s %s {\n"
  655. (org-taskjuggler-get-id project info)
  656. (org-taskjuggler-get-name project)
  657. ;; Version is obtained through :TASKJUGGLER_VERSION:
  658. ;; property or `org-taskjuggler-default-project-version'.
  659. (or (org-element-property :VERSION project)
  660. org-taskjuggler-default-project-version)
  661. (or (org-taskjuggler-get-start project)
  662. (format-time-string "%Y-%m-%d"))
  663. (let ((end (org-taskjuggler-get-end project)))
  664. (or (and end (format "- %s" end))
  665. (format "+%sd"
  666. org-taskjuggler-default-project-duration))))
  667. ;; Add attributes.
  668. (org-taskjuggler--indent-string
  669. (org-taskjuggler--build-attributes
  670. project org-taskjuggler-valid-project-attributes))
  671. ;; Closing project.
  672. "}\n"))
  673. (defun org-taskjuggler--build-resource (resource info)
  674. "Return a resource declaration.
  675. RESOURCE is a headline. INFO is a plist used as a communication
  676. channel.
  677. All valid attributes from RESOURCE are inserted. If RESOURCE
  678. defines a property \"resource_id\" it will be used as the id for
  679. this resource. Otherwise it will use the ID property. If
  680. neither is defined a unique id will be associated to it."
  681. (concat
  682. ;; Opening resource.
  683. (format "resource %s \"%s\" {\n"
  684. (org-taskjuggler--clean-id
  685. (or (org-element-property :RESOURCE_ID resource)
  686. (org-element-property :ID resource)
  687. (org-taskjuggler-get-id resource info)))
  688. (org-taskjuggler-get-name resource))
  689. ;; Add attributes.
  690. (org-taskjuggler--indent-string
  691. (org-taskjuggler--build-attributes
  692. resource org-taskjuggler-valid-resource-attributes))
  693. ;; Add inner resources.
  694. (org-taskjuggler--indent-string
  695. (mapconcat
  696. 'identity
  697. (org-element-map (org-element-contents resource) 'headline
  698. (lambda (hl) (org-taskjuggler--build-resource hl info))
  699. info nil 'headline)
  700. ""))
  701. ;; Closing resource.
  702. "}\n"))
  703. (defun org-taskjuggler--build-report (report info)
  704. "Return a report declaration.
  705. REPORT is a headline. INFO is a plist used as a communication
  706. channel."
  707. (concat
  708. ;; Opening report.
  709. (format "%s \"%s\" {\n"
  710. (or (org-element-property :REPORT_KIND report) "taskreport")
  711. (org-taskjuggler-get-name report))
  712. ;; Add attributes.
  713. (org-taskjuggler--indent-string
  714. (org-taskjuggler--build-attributes
  715. report org-taskjuggler-valid-report-attributes))
  716. ;; Add inner reports.
  717. (org-taskjuggler--indent-string
  718. (mapconcat
  719. 'identity
  720. (org-element-map (org-element-contents report) 'headline
  721. (lambda (hl) (org-taskjuggler--build-report hl info))
  722. info nil 'headline)
  723. ""))
  724. ;; Closing report.
  725. "}\n"))
  726. (defun org-taskjuggler--build-task (task info)
  727. "Return a task declaration.
  728. TASK is a headline. INFO is a plist used as a communication
  729. channel.
  730. All valid attributes from TASK are inserted. If TASK defines
  731. a property \"task_id\" it will be used as the id for this task.
  732. Otherwise it will use the ID property. If neither is defined
  733. a unique id will be associated to it."
  734. (let* ((allocate (org-element-property :ALLOCATE task))
  735. (complete
  736. (if (eq (org-element-property :todo-type task) 'done) "100"
  737. (org-element-property :COMPLETE task)))
  738. (depends (org-taskjuggler-resolve-dependencies task info))
  739. (effort (let ((property
  740. (intern (concat ":" (upcase org-effort-property)))))
  741. (org-element-property property task)))
  742. (milestone
  743. (or (org-element-property :MILESTONE task)
  744. (not (or (org-element-map (org-element-contents task) 'headline
  745. 'identity info t) ; Has task any child?
  746. effort
  747. (org-element-property :LENGTH task)
  748. (org-element-property :DURATION task)
  749. (and (org-taskjuggler-get-start task)
  750. (org-taskjuggler-get-end task))
  751. (org-element-property :PERIOD task)))))
  752. (priority
  753. (let ((pri (org-element-property :priority task)))
  754. (and pri
  755. (max 1 (/ (* 1000 (- org-lowest-priority pri))
  756. (- org-lowest-priority org-highest-priority)))))))
  757. (concat
  758. ;; Opening task.
  759. (format "task %s \"%s\" {\n"
  760. (org-taskjuggler-get-id task info)
  761. (org-taskjuggler-get-name task))
  762. ;; Add default attributes.
  763. (and depends
  764. (format " depends %s\n"
  765. (org-taskjuggler-format-dependencies depends task info)))
  766. (and allocate
  767. (format " purge %s\n allocate %s\n"
  768. ;; Compatibility for previous TaskJuggler versions.
  769. (if (>= org-taskjuggler-target-version 3.0) "allocate"
  770. "allocations")
  771. allocate))
  772. (and complete (format " complete %s\n" complete))
  773. (and effort
  774. (format " effort %s\n"
  775. (let* ((minutes (org-duration-string-to-minutes effort))
  776. (hours (/ minutes 60.0)))
  777. (format "%.1fh" hours))))
  778. (and priority (format " priority %s\n" priority))
  779. (and milestone " milestone\n")
  780. ;; Add other valid attributes.
  781. (org-taskjuggler--indent-string
  782. (org-taskjuggler--build-attributes
  783. task org-taskjuggler-valid-task-attributes))
  784. ;; Add inner tasks.
  785. (org-taskjuggler--indent-string
  786. (mapconcat 'identity
  787. (org-element-map (org-element-contents task) 'headline
  788. (lambda (hl) (org-taskjuggler--build-task hl info))
  789. info nil 'headline)
  790. ""))
  791. ;; Closing task.
  792. "}\n")))
  793. ;;; Interactive Functions
  794. ;;;###autoload
  795. (defun org-taskjuggler-export (&optional async subtreep visible-only)
  796. "Export current buffer to a TaskJuggler file.
  797. The exporter looks for a tree with tag that matches
  798. `org-taskjuggler-project-tag' and takes this as the tasks for
  799. this project. The first node of this tree defines the project
  800. properties such as project name and project period.
  801. If there is a tree with tag that matches
  802. `org-taskjuggler-resource-tag' this tree is taken as resources
  803. for the project. If no resources are specified, a default
  804. resource is created and allocated to the project.
  805. Also the TaskJuggler project will be created with default reports
  806. as defined in `org-taskjuggler-default-reports'.
  807. If narrowing is active in the current buffer, only export its
  808. narrowed part.
  809. If a region is active, export that region.
  810. A non-nil optional argument ASYNC means the process should happen
  811. asynchronously. The resulting file should be accessible through
  812. the `org-export-stack' interface.
  813. When optional argument SUBTREEP is non-nil, export the sub-tree
  814. at point, extracting information from the headline properties
  815. first.
  816. When optional argument VISIBLE-ONLY is non-nil, don't export
  817. contents of hidden elements.
  818. Return output file's name."
  819. (interactive)
  820. (let ((outfile
  821. (org-export-output-file-name org-taskjuggler-extension subtreep)))
  822. (org-export-to-file 'taskjuggler outfile
  823. async subtreep visible-only nil nil
  824. (lambda (file)
  825. (run-hook-with-args 'org-taskjuggler-final-hook file) nil))))
  826. ;;;###autoload
  827. (defun org-taskjuggler-export-and-process (&optional subtreep visible-only)
  828. "Export current buffer to a TaskJuggler file and process it.
  829. The exporter looks for a tree with tag that matches
  830. `org-taskjuggler-project-tag' and takes this as the tasks for
  831. this project. The first node of this tree defines the project
  832. properties such as project name and project period.
  833. If there is a tree with tag that matches
  834. `org-taskjuggler-resource-tag' this tree is taken as resources
  835. for the project. If no resources are specified, a default
  836. resource is created and allocated to the project.
  837. Also the TaskJuggler project will be created with default reports
  838. as defined in `org-taskjuggler-default-reports'.
  839. If narrowing is active in the current buffer, only export its
  840. narrowed part.
  841. If a region is active, export that region.
  842. When optional argument SUBTREEP is non-nil, export the sub-tree
  843. at point, extracting information from the headline properties
  844. first.
  845. When optional argument VISIBLE-ONLY is non-nil, don't export
  846. contents of hidden elements.
  847. Return a list of reports."
  848. (interactive)
  849. (let ((file (org-taskjuggler-export nil subtreep visible-only)))
  850. (org-taskjuggler-compile file)))
  851. ;;;###autoload
  852. (defun org-taskjuggler-export-process-and-open (&optional subtreep visible-only)
  853. "Export current buffer to a TaskJuggler file, process and open it.
  854. Export and process the file using
  855. `org-taskjuggler-export-and-process' and open the generated
  856. reports with a browser.
  857. If you are targeting TaskJuggler 2.4 (see
  858. `org-taskjuggler-target-version') the processing and display of
  859. the reports is done using the TaskJuggler GUI."
  860. (interactive)
  861. (if (< org-taskjuggler-target-version 3.0)
  862. (let* ((process-name "TaskJugglerUI")
  863. (command
  864. (concat process-name " "
  865. (org-taskjuggler-export nil subtreep visible-only))))
  866. (start-process-shell-command process-name nil command))
  867. (dolist (report (org-taskjuggler-export-and-process subtreep visible-only))
  868. (org-open-file report))))
  869. (defun org-taskjuggler-compile (file)
  870. "Compile a TaskJuggler file.
  871. FILE is the name of the file being compiled. Processing is done
  872. through the command given in `org-taskjuggler-process-command'.
  873. Return a list of reports."
  874. (let* ((full-name (file-truename file))
  875. (out-dir
  876. (expand-file-name
  877. org-taskjuggler-reports-directory (file-name-directory file)))
  878. errors)
  879. (message (format "Processing TaskJuggler file %s..." file))
  880. (save-window-excursion
  881. (let ((outbuf (get-buffer-create "*Org Taskjuggler Output*")))
  882. (unless (file-directory-p out-dir)
  883. (make-directory out-dir t))
  884. (with-current-buffer outbuf (erase-buffer))
  885. (shell-command
  886. (replace-regexp-in-string
  887. "%f" (shell-quote-argument full-name)
  888. (replace-regexp-in-string
  889. "%o" (shell-quote-argument out-dir)
  890. org-taskjuggler-process-command t t) t t) outbuf)
  891. ;; Collect standard errors from output buffer.
  892. (setq errors (org-taskjuggler--collect-errors outbuf)))
  893. (if (not errors)
  894. (message "Process completed.")
  895. (error (format "TaskJuggler failed with errors: %s" errors))))
  896. (file-expand-wildcards (format "%s/*.html" out-dir))))
  897. (defun org-taskjuggler--collect-errors (buffer)
  898. "Collect some kind of errors from \"tj3\" command output.
  899. BUFFER is the buffer containing output.
  900. Return collected error types as a string, or nil if there was
  901. none."
  902. (with-current-buffer buffer
  903. (save-excursion
  904. (goto-char (point-min))
  905. (let ((case-fold-search t)
  906. (errors ""))
  907. (while (re-search-forward "^.+:[0-9]+: \\(.*\\)$" nil t)
  908. (setq errors (concat errors " " (match-string 1))))
  909. (and (org-string-nw-p errors) (org-trim errors))))))
  910. (provide 'ox-taskjuggler)
  911. ;; Local variables:
  912. ;; sentence-end-double-space: t
  913. ;; End:
  914. ;;; ox-taskjuggler.el ends here