ox-taskjuggler.el 33 KB

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