ox-taskjuggler.el 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  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. '("taskreport \"Gantt Chart\" {
  195. headline \"Project Gantt Chart\"
  196. columns hierarchindex, name, start, end, effort, duration, completed, chart
  197. timeformat \"%Y-%m-%d\"
  198. hideresource 1
  199. loadunit shortauto
  200. }"
  201. "resourcereport \"Resource Graph\" {
  202. headline \"Resource Allocation Graph\"
  203. columns no, name, utilization, freeload, chart
  204. loadunit shortauto
  205. sorttasks startup
  206. hidetask ~isleaf()
  207. }")
  208. "Default reports for the project."
  209. :group 'org-export-taskjuggler
  210. :type '(repeat (string :tag "Report")))
  211. (defcustom org-taskjuggler-default-global-header ""
  212. "Default global header for the project.
  213. This goes before project declaration, and might be useful for
  214. early macros."
  215. :group 'org-export-taskjuggler
  216. :type '(string :tag "Preamble"))
  217. (defcustom org-taskjuggler-default-global-properties
  218. "shift s40 \"Part time shift\" {
  219. workinghours wed, thu, fri off
  220. }
  221. "
  222. "Default global properties for the project.
  223. Here you typically define global properties such as shifts,
  224. accounts, rates, vacation, macros and flags. Any property that
  225. is allowed within the TaskJuggler file can be inserted. You
  226. could for example include another TaskJuggler file.
  227. The global properties are inserted after the project declaration
  228. but before any resource and task declarations."
  229. :group 'org-export-taskjuggler
  230. :type '(string :tag "Preamble"))
  231. (defcustom org-taskjuggler-valid-task-attributes
  232. '(account start note duration endbuffer endcredit end
  233. flags journalentry length limits maxend maxstart minend
  234. minstart period reference responsible scheduling
  235. startbuffer startcredit statusnote chargeset charge)
  236. "Valid attributes for Taskjuggler tasks.
  237. If one of these appears as a property for a headline, it will be
  238. exported with the corresponding task."
  239. :group 'org-export-taskjuggler)
  240. (defcustom org-taskjuggler-valid-resource-attributes
  241. '(limits vacation shift booking efficiency journalentry rate
  242. workinghours flags)
  243. "Valid attributes for Taskjuggler resources.
  244. If one of these appears as a property for a headline, it will be
  245. exported with the corresponding resource."
  246. :group 'org-export-taskjuggler)
  247. (defcustom org-taskjuggler-valid-report-attributes
  248. '(headline columns definitions timeformat hideresource hidetask
  249. loadunit sorttasks formats period)
  250. "Valid attributes for Taskjuggler reports.
  251. If one of these appears as a property for a headline, it will be
  252. exported with the corresponding report."
  253. :group 'org-export-taskjuggler)
  254. (defcustom org-taskjuggler-keep-project-as-task t
  255. "Non-nil keeps the project headline as an umbrella task for all tasks.
  256. Setting this to nil will allow maintaining completely separated
  257. task buckets, while still sharing the same resources pool."
  258. :group 'org-export-taskjuggler
  259. :type 'boolean)
  260. ;;; Hooks
  261. (defvar org-taskjuggler-final-hook nil
  262. "Hook run after a TaskJuggler files has been saved.
  263. This hook is run with the name of the file as argument.")
  264. ;;; Back-End Definition
  265. (org-export-define-backend 'taskjuggler
  266. '((template . org-taskjuggler-project-plan))
  267. :menu-entry
  268. '(?J "Export to TaskJuggler"
  269. ((?j "As TJP file" (lambda (a s v b) (org-taskjuggler-export a s v)))
  270. (?o "As TJP file and open"
  271. (lambda (a s v b)
  272. (if a (org-taskjuggler-export a s v)
  273. (org-taskjuggler-export-and-open s v))))))
  274. ;; This property will be used to store unique ids in communication
  275. ;; channel. Ids will be retrieved with `org-taskjuggler-get-id'.
  276. :options-alist '((:taskjuggler-unique-ids nil nil nil)))
  277. ;;; Unique IDs
  278. (defun org-taskjuggler-assign-task-ids (tasks info)
  279. "Assign a unique ID to each task in TASKS.
  280. TASKS is a list of headlines. Return value is an alist between
  281. headlines and their associated ID. IDs are hierarchical, which
  282. means they only need to be unique among the task siblings."
  283. (let* (alist
  284. (build-id
  285. (lambda (tasks local-ids)
  286. (org-element-map tasks 'headline
  287. (lambda (task)
  288. (let ((id (org-taskjuggler--build-unique-id task local-ids)))
  289. (push id local-ids)
  290. (push (cons task id) alist)
  291. (funcall build-id (org-element-contents task) nil)))
  292. info nil 'headline))))
  293. (funcall build-id tasks nil)
  294. alist))
  295. (defun org-taskjuggler-assign-resource-ids (resources info)
  296. "Assign a unique ID to each resource within RESOURCES.
  297. RESOURCES is a list of headlines. Return value is an alist
  298. between headlines and their associated ID."
  299. (let (ids)
  300. (org-element-map resources 'headline
  301. (lambda (resource)
  302. (let ((id (org-taskjuggler--build-unique-id resource ids)))
  303. (push id ids)
  304. (cons resource id)))
  305. info)))
  306. ;;; Accessors
  307. (defun org-taskjuggler-get-project (info)
  308. "Return project in parse tree.
  309. INFO is a plist used as a communication channel. First headline
  310. in buffer with `org-taskjuggler-project-tag' defines the project.
  311. If no such task is defined, pick the first headline in buffer.
  312. If there is no headline at all, return nil."
  313. (or (org-element-map (plist-get info :parse-tree) 'headline
  314. (lambda (hl)
  315. (and (member org-taskjuggler-project-tag
  316. (org-export-get-tags hl info))
  317. hl))
  318. info t)
  319. (org-element-map tree 'headline 'identity info t)))
  320. (defun org-taskjuggler-get-id (item info)
  321. "Return id for task or resource ITEM.
  322. ITEM is a headline. Return value is a string."
  323. (cdr (assq item (plist-get info :taskjuggler-unique-ids))))
  324. (defun org-taskjuggler-get-name (item)
  325. "Return name for task or resource ITEM.
  326. ITEM is a headline. Return value is a string."
  327. ;; Quote double quotes in name.
  328. (replace-regexp-in-string
  329. "\"" "\\\"" (org-element-property :raw-value item) t t))
  330. (defun org-taskjuggler-get-start (item)
  331. "Return start date for task or resource ITEM.
  332. ITEM is a headline. Return value is a string or nil if ITEM
  333. doesn't have any start date defined.."
  334. (let ((scheduled (org-element-property :scheduled item)))
  335. (and scheduled (org-timestamp-format scheduled "%Y-%02m-%02d"))))
  336. (defun org-taskjuggler-get-end (item)
  337. "Return end date for task or resource ITEM.
  338. ITEM is a headline. Return value is a string or nil if ITEM
  339. doesn't have any end date defined.."
  340. (let ((deadline (org-element-property :deadline item)))
  341. (and deadline (org-timestamp-format deadline "%Y-%02m-%02d"))))
  342. ;;; Internal Functions
  343. (defun org-taskjuggler--indent-string (s)
  344. "Indent string S by 2 spaces.
  345. Return new string. If S is the empty string, return it."
  346. (if (equal "" s) s (replace-regexp-in-string "^ *\\S-" " \\&" s)))
  347. (defun org-taskjuggler--build-attributes (item attributes)
  348. "Return attributes string for task, resource or report ITEM.
  349. ITEM is a headline. ATTRIBUTES is a list of symbols
  350. representing valid attributes for ITEM."
  351. (mapconcat
  352. (lambda (attribute)
  353. (let ((value (org-element-property
  354. (intern (upcase (format ":%s" attribute)))
  355. item)))
  356. (and value (format "%s %s\n" attribute value))))
  357. (remq nil attributes) ""))
  358. (defun org-taskjuggler--build-unique-id (item unique-ids)
  359. "Return a unique id for a given task or a resource.
  360. ITEM is an `headline' type element representing the task or
  361. resource. Its id is derived from its name and made unique
  362. against UNIQUE-IDS. If the (downcased) first token of the
  363. headline is not unique try to add more (downcased) tokens of the
  364. headline or finally add more underscore characters (\"_\")."
  365. (let ((id (org-string-nw-p (org-element-property :TASK_ID item))))
  366. ;; If an id is specified, use it, as long as it's unique.
  367. (if (and id (not (member id unique-ids))) id
  368. (let* ((parts (org-split-string (org-element-property :raw-value item)))
  369. (id (org-taskjuggler--clean-id (downcase (pop parts)))))
  370. ;; Try to add more parts of the headline to make it unique.
  371. (while (and (car parts) (member id unique-ids))
  372. (setq id (concat id "_"
  373. (org-taskjuggler--clean-id (downcase (pop parts))))))
  374. ;; If it's still not unique, add "_".
  375. (while (member id unique-ids)
  376. (setq id (concat id "_")))
  377. id))))
  378. (defun org-taskjuggler--clean-id (id)
  379. "Clean and return ID to make it acceptable for TaskJuggler.
  380. ID is a string."
  381. ;; Replace non-ascii by "_".
  382. (replace-regexp-in-string
  383. "[^a-zA-Z0-9_]" "_"
  384. ;; Make sure id doesn't start with a number.
  385. (replace-regexp-in-string "^\\([0-9]\\)" "_\\1" id)))
  386. ;;; Dependencies
  387. (defun org-taskjuggler-resolve-dependencies (task info)
  388. "Return a list of all tasks TASK depends on.
  389. TASK is a headline. INFO is a plist used as a communication
  390. channel."
  391. (let ((deps-ids
  392. ;; Get all dependencies specified in BLOCKER and DEPENDS task
  393. ;; properties. Clean options from them.
  394. (let ((deps (concat (org-element-property :BLOCKER task)
  395. (org-element-property :DEPENDS task))))
  396. (and deps
  397. (org-split-string (replace-regexp-in-string "{.*?}" "" deps)
  398. "[ ,]* +"))))
  399. depends)
  400. (when deps-ids
  401. ;; Find tasks with :task_id: property matching id in DEPS-IDS.
  402. ;; Add them to DEPENDS.
  403. (let* ((project (org-taskjuggler-get-project info))
  404. (tasks (if org-taskjuggler-keep-project-as-task project
  405. (org-element-contents project))))
  406. (setq depends
  407. (org-element-map tasks 'headline
  408. (lambda (task)
  409. (let ((task-id (org-element-property :TASK_ID task)))
  410. (and task-id (member task-id deps-ids) task)))
  411. info)))
  412. ;; Check BLOCKER and DEPENDS properties. If "previous-sibling"
  413. ;; belongs to DEPS-ID, add it to DEPENDS.
  414. (when (and (member-ignore-case "previous-sibling" deps-ids)
  415. (not (org-export-first-sibling-p task info)))
  416. (let ((prev (org-export-get-previous-element task info)))
  417. (and (not (memq prev depends)) (push prev depends)))))
  418. ;; Check ORDERED status of parent.
  419. (let ((parent (org-export-get-parent task)))
  420. (when (and parent
  421. (org-element-property :ORDERED parent)
  422. (not (org-export-first-sibling-p task info)))
  423. (push (org-export-get-previous-element task info) depends)))
  424. ;; Return dependencies.
  425. depends))
  426. (defun org-taskjuggler-format-dependencies (dependencies task info)
  427. "Format DEPENDENCIES to match TaskJuggler syntax.
  428. DEPENDENCIES is list of dependencies for TASK, as returned by
  429. `org-taskjuggler-resolve-depedencies'. TASK is a headline.
  430. INFO is a plist used as a communication channel. Return value
  431. doesn't include leading \"depends\"."
  432. (let ((dep-str (concat (org-element-property :BLOCKER task)
  433. " "
  434. (org-element-property :DEPENDS task)))
  435. (get-path
  436. (lambda (dep)
  437. ;; Return path to DEP relatively to TASK.
  438. (let ((parent (org-export-get-parent dep))
  439. (exclamations 1)
  440. (option
  441. (let ((id (org-element-property :TASK_ID dep)))
  442. (and id
  443. (string-match (concat id " +\\({.*?}\\)") dep-str)
  444. (org-match-string-no-properties 1))))
  445. path)
  446. ;; Compute number of exclamation marks by looking for the
  447. ;; common ancestor between TASK and DEP.
  448. (while (not (org-element-map parent 'headline
  449. (lambda (hl) (eq hl task))))
  450. (incf exclamations)
  451. (setq parent (org-export-get-parent parent)))
  452. ;; Build path from DEP to PARENT.
  453. (while (not (eq parent dep))
  454. (push (org-taskjuggler-get-id dep info) path)
  455. (setq dep (org-export-get-parent dep)))
  456. ;; Return full path. Add dependency options, if any.
  457. (concat (make-string exclamations ?!)
  458. (mapconcat 'identity path ".")
  459. (and option (concat " " option)))))))
  460. ;; Return dependencies string, without the leading "depends".
  461. (mapconcat (lambda (dep) (funcall get-path dep)) dependencies ", ")))
  462. ;;; Translator Functions
  463. (defun org-taskjuggler-project-plan (contents info)
  464. "Build TaskJuggler project plan.
  465. CONTENTS is ignored. INFO is a plist holding export options.
  466. Return complete project plan as a string in TaskJuggler syntax."
  467. (let* ((tree (plist-get info :parse-tree))
  468. (project (or (org-taskjuggler-get-project info)
  469. (error "No project specified"))))
  470. (concat
  471. ;; 1. Insert header.
  472. (org-element-normalize-string org-taskjuggler-default-global-header)
  473. ;; 2. Insert project.
  474. (org-taskjuggler--build-project project info)
  475. ;; 3. Insert global properties.
  476. (org-element-normalize-string org-taskjuggler-default-global-properties)
  477. ;; 4. Insert resources. Provide a default one if none is
  478. ;; specified.
  479. (let ((main-resources
  480. ;; Collect contents from various trees marked with
  481. ;; `org-taskjuggler-resource-tag'. Only gather top level
  482. ;; resources.
  483. (apply 'append
  484. (org-element-map tree 'headline
  485. (lambda (hl)
  486. (and (member org-taskjuggler-resource-tag
  487. (org-export-get-tags hl info))
  488. (org-element-map (org-element-contents hl) 'headline
  489. 'identity info nil 'headline)))
  490. info nil 'headline))))
  491. ;; Assign a unique ID to each resource. Store it under
  492. ;; `:taskjuggler-unique-ids' property in INFO.
  493. (setq info
  494. (plist-put info :taskjuggler-unique-ids
  495. (org-taskjuggler-assign-resource-ids
  496. main-resources info)))
  497. (concat
  498. (if main-resources
  499. (mapconcat
  500. (lambda (resource) (org-taskjuggler--build-resource resource info))
  501. main-resources "")
  502. (format "resource %s \"%s\" {\n}\n" (user-login-name) user-full-name))
  503. ;; 5. Insert tasks.
  504. (let ((main-tasks
  505. ;; If `org-taskjuggler-keep-project-as-task' is
  506. ;; non-nil, there is only one task. Otherwise, every
  507. ;; direct children of PROJECT is a top level task.
  508. (if org-taskjuggler-keep-project-as-task (list project)
  509. (or (org-element-map (org-element-contents project) 'headline
  510. 'identity info nil 'headline)
  511. (error "No task specified")))))
  512. ;; Assign a unique ID to each task. Add it to
  513. ;; `:taskjuggler-unique-ids' property in INFO.
  514. (setq info
  515. (plist-put info :taskjuggler-unique-ids
  516. (append
  517. (org-taskjuggler-assign-task-ids main-tasks info)
  518. (plist-get info :taskjuggler-unique-ids))))
  519. ;; If no resource is allocated among tasks, allocate one to
  520. ;; the first task.
  521. (unless (org-element-map main-tasks 'headline
  522. (lambda (task) (org-element-property :ALLOCATE task))
  523. info t)
  524. (org-element-put-property
  525. (car main-tasks) :ALLOCATE
  526. (or (org-taskjuggler-get-id (car main-resources) info)
  527. (user-login-name))))
  528. (mapconcat
  529. (lambda (task) (org-taskjuggler--build-task task info))
  530. main-tasks ""))
  531. ;; 6. Insert reports. If no report is defined, insert default
  532. ;; reports.
  533. (let ((main-reports
  534. ;; Collect contents from various trees marked with
  535. ;; `org-taskjuggler-report-tag'. Only gather top level
  536. ;; reports.
  537. (apply 'append
  538. (org-element-map tree 'headline
  539. (lambda (hl)
  540. (and (member org-taskjuggler-report-tag
  541. (org-export-get-tags hl info))
  542. (org-element-map (org-element-contents hl)
  543. 'headline 'identity info nil 'headline)))
  544. info nil 'headline))))
  545. (if main-reports
  546. (mapconcat
  547. (lambda (report) (org-taskjuggler--build-report report info))
  548. main-reports "")
  549. (mapconcat 'org-element-normalize-string
  550. org-taskjuggler-default-reports ""))))))))
  551. (defun org-taskjuggler--build-project (project info)
  552. "Return a project declaration.
  553. PROJECT is a headline. INFO is a plist used as a communication
  554. channel. If no start date is specified, start today. If no end
  555. date is specified, end `org-taskjuggler-default-project-duration'
  556. days from now."
  557. (format "project %s \"%s\" \"%s\" %s %s {\n}\n"
  558. (org-taskjuggler-get-id project info)
  559. (org-taskjuggler-get-name project)
  560. ;; Version is obtained through :TASKJUGGLER_VERSION:
  561. ;; property or `org-taskjuggler-default-project-version'.
  562. (or (org-element-property :VERSION project)
  563. org-taskjuggler-default-project-version)
  564. (or (org-taskjuggler-get-start project)
  565. (format-time-string "%Y-%m-%d"))
  566. (let ((end (org-taskjuggler-get-end project)))
  567. (or (and end (format "- %s" end))
  568. (format "+%sd" org-taskjuggler-default-project-duration)))))
  569. (defun org-taskjuggler--build-resource (resource info)
  570. "Return a resource declaration.
  571. RESOURCE is a headline. INFO is a plist used as a communication
  572. channel.
  573. All valid attributes from RESOURCE are inserted. If RESOURCE
  574. defines a property \"resource_id\" it will be used as the id for
  575. this resource. Otherwise it will use the ID property. If
  576. neither is defined a unique id will be associated to it."
  577. (concat
  578. ;; Opening resource.
  579. (format "resource %s \"%s\" {\n"
  580. (org-taskjuggler--clean-id
  581. (or (org-element-property :RESOURCE_ID resource)
  582. (org-element-property :ID resource)
  583. (org-taskjuggler-get-id resource info)))
  584. (org-taskjuggler-get-name resource))
  585. ;; Add attributes.
  586. (org-taskjuggler--indent-string
  587. (org-taskjuggler--build-attributes
  588. resource org-taskjuggler-valid-resource-attributes))
  589. ;; Add inner resources.
  590. (org-taskjuggler--indent-string
  591. (mapconcat
  592. 'identity
  593. (org-element-map (org-element-contents resource) 'headline
  594. (lambda (hl) (org-taskjuggler--build-resource hl info))
  595. info nil 'headline)
  596. ""))
  597. ;; Closing resource.
  598. "}\n"))
  599. (defun org-taskjuggler--build-report (report)
  600. "Return a report declaration.
  601. REPORT is a headline. INFO is a plist used as a communication
  602. channel."
  603. (concat
  604. ;; Opening report.
  605. (format "%s \"%s\" {\n"
  606. (or (org-element-property :REPORT_KIND report) "taskreport")
  607. (org-taskjuggler-get-name report))
  608. ;; Add attributes.
  609. (org-taskjuggler--indent-string
  610. (org-taskjuggler--build-attributes
  611. report org-taskjuggler-valid-report-attributes))
  612. ;; Add inner reports.
  613. (org-taskjuggler--indent-string
  614. (mapconcat
  615. 'identity
  616. (org-element-map (org-element-contents report) 'headline
  617. (lambda (hl) (org-taskjuggler--build-report hl info))
  618. info nil 'headline)
  619. ""))
  620. ;; Closing report.
  621. "}\n"))
  622. (defun org-taskjuggler--build-task (task info)
  623. "Return a task declaration.
  624. TASK is a headline. INFO is a plist used as a communication
  625. channel.
  626. All valid attributes from TASK are inserted. If TASK defines
  627. a property \"task_id\" it will be used as the id for this task.
  628. Otherwise it will use the ID property. If neither is defined
  629. a unique id will be associated to it."
  630. (let* ((allocate (org-element-property :ALLOCATE task))
  631. (complete
  632. (if (eq (org-element-property :todo-type task) 'done) "100"
  633. (org-element-property :COMPLETE task)))
  634. (depends (org-taskjuggler-resolve-dependencies task info))
  635. (effort (org-element-property :EFFORT task))
  636. (milestone
  637. (or (org-element-property :MILESTONE task)
  638. (not (or (org-element-map (org-element-contents task) 'headline
  639. 'identity info t) ; Has task any child?
  640. effort
  641. (org-element-property :LENGTH task)
  642. (org-element-property :DURATION task)
  643. (and (org-taskjuggler-get-start task)
  644. (org-taskjuggler-get-end task))
  645. (org-element-property :PERIOD task)))))
  646. (priority
  647. (let ((pri (org-element-property :priority task)))
  648. (and pri
  649. (max 1 (/ (* 1000 (- org-lowest-priority pri))
  650. (- org-lowest-priority org-highest-priority)))))))
  651. (concat
  652. ;; Opening task.
  653. (format "task %s \"%s\" {\n"
  654. (org-taskjuggler-get-id task info)
  655. (org-taskjuggler-get-name task))
  656. ;; Add default attributes.
  657. (and depends
  658. (format " depends %s\n"
  659. (org-taskjuggler-format-dependencies depends task info)))
  660. (and allocate
  661. (format " purge %s\n allocate %s\n"
  662. ;; Compatibility for previous TaskJuggler versions.
  663. (if (>= org-taskjuggler-target-version 3.0) "allocate"
  664. "allocations")
  665. allocate))
  666. (and complete (format " complete %s\n" comptete))
  667. (and effort
  668. (format " effort %s\n"
  669. (let* ((minutes (org-duration-string-to-minutes effort))
  670. (hours (/ minutes 60.0)))
  671. (format "%.1fh" hours))))
  672. (and priority (format " priority %s\n" complete))
  673. (and milestone " milestone\n")
  674. ;; Add other valid attributes.
  675. (org-taskjuggler--indent-string
  676. (org-taskjuggler--build-attributes
  677. task org-taskjuggler-valid-task-attributes))
  678. ;; Add inner tasks.
  679. (org-taskjuggler--indent-string
  680. (mapconcat 'identity
  681. (org-element-map (org-element-contents task) 'headline
  682. (lambda (hl) (org-taskjuggler--build-task hl info))
  683. info nil 'headline)
  684. ""))
  685. ;; Closing task.
  686. "}\n")))
  687. ;;; Interactive Functions
  688. ;;;###autoload
  689. (defun org-taskjuggler-export (&optional async subtreep visible-only)
  690. "Export current buffer to a TaskJuggler file.
  691. The exporter looks for a tree with tag that matches
  692. `org-taskjuggler-project-tag' and takes this as the tasks for
  693. this project. The first node of this tree defines the project
  694. properties such as project name and project period.
  695. If there is a tree with tag that matches
  696. `org-taskjuggler-resource-tag' this tree is taken as resources
  697. for the project. If no resources are specified, a default
  698. resource is created and allocated to the project.
  699. Also the TaskJuggler project will be created with default reports
  700. as defined in `org-taskjuggler-default-reports'.
  701. If narrowing is active in the current buffer, only export its
  702. narrowed part.
  703. If a region is active, export that region.
  704. A non-nil optional argument ASYNC means the process should happen
  705. asynchronously. The resulting file should be accessible through
  706. the `org-export-stack' interface.
  707. When optional argument SUBTREEP is non-nil, export the sub-tree
  708. at point, extracting information from the headline properties
  709. first.
  710. When optional argument VISIBLE-ONLY is non-nil, don't export
  711. contents of hidden elements.
  712. Return output file's name."
  713. (interactive)
  714. (let ((outfile
  715. (org-export-output-file-name org-taskjuggler-extension subtreep)))
  716. (if async
  717. (org-export-async-start
  718. (lambda (f)
  719. (org-export-add-to-stack f 'taskjuggler)
  720. (run-hook-with-args 'org-taskjuggler-final-hook f))
  721. `(expand-file-name
  722. (org-export-to-file 'taskjuggler ,outfile ,subtreep ,visible-only)))
  723. (org-export-to-file 'taskjuggler outfile subtreep visible-only)
  724. (run-hook-with-args 'org-taskjuggler-final-hook outfile)
  725. outfile)))
  726. ;;;###autoload
  727. (defun org-taskjuggler-export-and-open (&optional subtreep visible-only)
  728. "Export current buffer to a TaskJuggler file and open it.
  729. The exporter looks for a tree with tag that matches
  730. `org-taskjuggler-project-tag' and takes this as the tasks for
  731. this project. The first node of this tree defines the project
  732. properties such as project name and project period.
  733. If there is a tree with tag that matches
  734. `org-taskjuggler-resource-tag' this tree is taken as resources
  735. for the project. If no resources are specified, a default
  736. resource is created and allocated to the project.
  737. Also the TaskJuggler project will be created with default reports
  738. as defined in `org-taskjuggler-default-reports'.
  739. If narrowing is active in the current buffer, only export its
  740. narrowed part.
  741. If a region is active, export that region.
  742. When optional argument SUBTREEP is non-nil, export the sub-tree
  743. at point, extracting information from the headline properties
  744. first.
  745. When optional argument VISIBLE-ONLY is non-nil, don't export
  746. contents of hidden elements.
  747. Open file with the TaskJuggler GUI."
  748. (interactive)
  749. (let* ((file (org-taskjuggler-export nil subtreep visible-only))
  750. (process-name "TaskJugglerUI")
  751. (command (concat process-name " " file)))
  752. (start-process-shell-command process-name nil command)))
  753. (provide 'ox-taskjuggler)
  754. ;; Local variables:
  755. ;; sentence-end-double-space: t
  756. ;; End:
  757. ;;; ox-taskjuggler.el ends here