ox-taskjuggler.el 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  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* ((parts (org-split-string (org-element-property :raw-value item)))
  361. (id (org-taskjuggler--clean-id (downcase (pop parts)))))
  362. ;; Try to add more parts of the headline to make it unique.
  363. (while (and (car parts) (member id unique-ids))
  364. (setq id (concat id "_"
  365. (org-taskjuggler--clean-id (downcase (pop parts))))))
  366. ;; If it's still not unique, add "_".
  367. (while (member id unique-ids)
  368. (setq id (concat id "_")))
  369. id))
  370. (defun org-taskjuggler--clean-id (id)
  371. "Clean and return ID to make it acceptable for TaskJuggler.
  372. ID is a string."
  373. ;; Replace non-ascii by "_".
  374. (replace-regexp-in-string
  375. "[^a-zA-Z0-9_]" "_"
  376. ;; Make sure id doesn't start with a number.
  377. (replace-regexp-in-string "^\\([0-9]\\)" "_\\1" id)))
  378. ;;; Dependencies
  379. (defun org-taskjuggler-resolve-dependencies (task info)
  380. "Return a list of all tasks TASK depends on.
  381. TASK is a headline. INFO is a plist used as a communication
  382. channel."
  383. (let ((deps-ids
  384. ;; Get all dependencies specified in BLOCKER and DEPENDS task
  385. ;; properties. Clean options from them.
  386. (let ((deps (concat (org-element-property :BLOCKER task)
  387. (org-element-property :DEPENDS task))))
  388. (and deps
  389. (org-split-string (replace-regexp-in-string "{.*?}" "" deps)
  390. "[ ,]* +"))))
  391. depends)
  392. (when deps-ids
  393. ;; Find tasks with :task_id: property matching id in DEPS-IDS.
  394. ;; Add them to DEPENDS.
  395. (let* ((project (org-taskjuggler-get-project info))
  396. (tasks (if org-taskjuggler-keep-project-as-task project
  397. (org-element-contents project))))
  398. (setq depends
  399. (org-element-map tasks 'headline
  400. (lambda (task)
  401. (let ((task-id (org-element-property :TASK_ID task)))
  402. (and task-id (member task-id deps-ids) task)))
  403. info)))
  404. ;; Check BLOCKER and DEPENDS properties. If "previous-sibling"
  405. ;; belongs to DEPS-ID, add it to DEPENDS.
  406. (when (and (member-ignore-case "previous-sibling" deps-ids)
  407. (not (org-export-first-sibling-p task info)))
  408. (let ((prev (org-export-get-previous-element task info)))
  409. (and (not (memq prev depends)) (push prev depends)))))
  410. ;; Check ORDERED status of parent.
  411. (let ((parent (org-export-get-parent task)))
  412. (when (and parent
  413. (org-element-property :ORDERED parent)
  414. (not (org-export-first-sibling-p task info)))
  415. (push (org-export-get-previous-element task info) depends)))
  416. ;; Return dependencies.
  417. depends))
  418. (defun org-taskjuggler-format-dependencies (dependencies task info)
  419. "Format DEPENDENCIES to match TaskJuggler syntax.
  420. DEPENDENCIES is list of dependencies for TASK, as returned by
  421. `org-taskjuggler-resolve-depedencies'. TASK is a headline.
  422. INFO is a plist used as a communication channel. Return value
  423. doesn't include leading \"depends\"."
  424. (let ((dep-str (concat (org-element-property :BLOCKER task)
  425. " "
  426. (org-element-property :DEPENDS task)))
  427. (get-path
  428. (lambda (dep)
  429. ;; Return path to DEP relatively to TASK.
  430. (let ((parent (org-export-get-parent dep))
  431. (exclamations 1)
  432. (option
  433. (let ((id (org-element-property :TASK_ID dep)))
  434. (and id
  435. (string-match (concat id " +\\({.*?}\\)") dep-str)
  436. (org-match-string-no-properties 1))))
  437. path)
  438. ;; Compute number of exclamation marks.
  439. (while (not (org-element-map parent 'headline
  440. (lambda (task) (eq task dep))))
  441. (incf exclamations)
  442. (setq parent (org-export-get-parent parent)))
  443. ;; Build path from DEP to PARENT.
  444. (while (not (eq parent dep))
  445. (push (org-taskjuggler-get-id dep info) path)
  446. (setq dep (org-export-get-parent dep)))
  447. ;; Return full path. Add dependency options, if any.
  448. (concat (make-string exclamations ?!)
  449. (mapconcat 'identity path ".")
  450. (and option (concat " " option)))))))
  451. ;; Return dependencies string, without the leading "depends".
  452. (mapconcat (lambda (dep) (funcall get-path dep)) dependencies ", ")))
  453. ;;; Translator Functions
  454. (defun org-taskjuggler-project-plan (contents info)
  455. "Build TaskJuggler project plan.
  456. CONTENTS is ignored. INFO is a plist holding export options.
  457. Return complete project plan as a string in TaskJuggler syntax."
  458. (let* ((tree (plist-get info :parse-tree))
  459. (project (or (org-taskjuggler-get-project info)
  460. (error "No project specified"))))
  461. (concat
  462. ;; 1. Insert header.
  463. (org-element-normalize-string org-taskjuggler-default-global-header)
  464. ;; 2. Insert project.
  465. (org-taskjuggler--build-project project info)
  466. ;; 3. Insert global properties.
  467. (org-element-normalize-string org-taskjuggler-default-global-properties)
  468. ;; 4. Insert resources. Provide a default one if none is
  469. ;; specified.
  470. (let ((main-resources
  471. ;; Collect contents from various trees marked with
  472. ;; `org-taskjuggler-resource-tag'. Only gather top level
  473. ;; resources.
  474. (apply 'append
  475. (org-element-map tree 'headline
  476. (lambda (hl)
  477. (and (member org-taskjuggler-resource-tag
  478. (org-export-get-tags hl info))
  479. (org-element-map (org-element-contents hl) 'headline
  480. 'identity info nil 'headline)))
  481. info nil 'headline))))
  482. ;; Assign a unique ID to each resource. Store it under
  483. ;; `:taskjuggler-unique-ids' property in INFO.
  484. (setq info
  485. (plist-put info :taskjuggler-unique-ids
  486. (org-taskjuggler-assign-resource-ids
  487. main-resources info)))
  488. (concat
  489. (if main-resources
  490. (mapconcat
  491. (lambda (resource) (org-taskjuggler--build-resource resource info))
  492. main-resources "")
  493. (format "resource %s \"%s\" {\n}\n" (user-login-name) user-full-name))
  494. ;; 5. Insert tasks.
  495. (let ((main-tasks
  496. ;; If `org-taskjuggler-keep-project-as-task' is
  497. ;; non-nil, there is only one task. Otherwise, every
  498. ;; direct children of PROJECT is a top level task.
  499. (if org-taskjuggler-keep-project-as-task (list project)
  500. (or (org-element-map (org-element-contents project) 'headline
  501. 'identity info nil 'headline)
  502. (error "No task specified")))))
  503. ;; Assign a unique ID to each task. Add it to
  504. ;; `:taskjuggler-unique-ids' property in INFO.
  505. (setq info
  506. (plist-put info :taskjuggler-unique-ids
  507. (append
  508. (org-taskjuggler-assign-task-ids main-tasks info)
  509. (plist-get info :taskjuggler-unique-ids))))
  510. ;; If no resource is allocated among tasks, allocate one to
  511. ;; the first task.
  512. (unless (org-element-map main-tasks 'headline
  513. (lambda (task) (org-element-property :ALLOCATE task))
  514. info t)
  515. (org-element-put-property
  516. (car main-tasks) :ALLOCATE
  517. (or (org-taskjuggler-get-id (car main-resources) info)
  518. (user-login-name))))
  519. (mapconcat
  520. (lambda (task) (org-taskjuggler--build-task task info))
  521. main-tasks ""))
  522. ;; 6. Insert reports. If no report is defined, insert default
  523. ;; reports.
  524. (let ((main-reports
  525. ;; Collect contents from various trees marked with
  526. ;; `org-taskjuggler-report-tag'. Only gather top level
  527. ;; reports.
  528. (apply 'append
  529. (org-element-map tree 'headline
  530. (lambda (hl)
  531. (and (member org-taskjuggler-report-tag
  532. (org-export-get-tags hl info))
  533. (org-element-map (org-element-contents hl)
  534. 'headline 'identity info nil 'headline)))
  535. info nil 'headline))))
  536. (if main-reports
  537. (mapconcat
  538. (lambda (report) (org-taskjuggler--build-report report info))
  539. main-reports "")
  540. (mapconcat 'org-element-normalize-string
  541. org-taskjuggler-default-reports ""))))))))
  542. (defun org-taskjuggler--build-project (project info)
  543. "Return a project declaration.
  544. PROJECT is a headline. INFO is a plist used as a communication
  545. channel. If no start date is specified, start today. If no end
  546. date is specified, end `org-taskjuggler-default-project-duration'
  547. days from now."
  548. (format "project %s \"%s\" \"%s\" %s %s {\n}\n"
  549. (org-taskjuggler-get-id project info)
  550. (org-taskjuggler-get-name project)
  551. ;; Version is obtained through :TASKJUGGLER_VERSION:
  552. ;; property or `org-taskjuggler-default-project-version'.
  553. (or (org-element-property :VERSION project)
  554. org-taskjuggler-default-project-version)
  555. (or (org-taskjuggler-get-start project)
  556. (format-time-string "%Y-%m-%d"))
  557. (let ((end (org-taskjuggler-get-end project)))
  558. (or (and end (format "- %s" end))
  559. (format "+%sd" org-taskjuggler-default-project-duration)))))
  560. (defun org-taskjuggler--build-resource (resource info)
  561. "Return a resource declaration.
  562. RESOURCE is a headline. INFO is a plist used as a communication
  563. channel.
  564. All valid attributes from RESOURCE are inserted. If RESOURCE
  565. defines a property \"resource_id\" it will be used as the id for
  566. this resource. Otherwise it will use the ID property. If
  567. neither is defined a unique id will be associated to it."
  568. (concat
  569. ;; Opening resource.
  570. (format "resource %s \"%s\" {\n"
  571. (org-taskjuggler--clean-id
  572. (or (org-element-property :RESOURCE_ID resource)
  573. (org-element-property :ID resource)
  574. (org-taskjuggler-get-id resource info)))
  575. (org-taskjuggler-get-name resource))
  576. ;; Add attributes.
  577. (org-taskjuggler--indent-string
  578. (org-taskjuggler--build-attributes
  579. resource org-taskjuggler-valid-resource-attributes))
  580. ;; Add inner resources.
  581. (org-taskjuggler--indent-string
  582. (mapconcat
  583. 'identity
  584. (org-element-map (org-element-contents resource) 'headline
  585. (lambda (hl) (org-taskjuggler--build-resource hl info))
  586. info nil 'headline)
  587. ""))
  588. ;; Closing resource.
  589. "}\n"))
  590. (defun org-taskjuggler--build-report (report)
  591. "Return a report declaration.
  592. REPORT is a headline. INFO is a plist used as a communication
  593. channel."
  594. (concat
  595. ;; Opening report.
  596. (format "%s \"%s\" {\n"
  597. (or (org-element-property :REPORT_KIND report) "taskreport")
  598. (org-taskjuggler-get-name report))
  599. ;; Add attributes.
  600. (org-taskjuggler--indent-string
  601. (org-taskjuggler--build-attributes
  602. report org-taskjuggler-valid-report-attributes))
  603. ;; Add inner reports.
  604. (org-taskjuggler--indent-string
  605. (mapconcat
  606. 'identity
  607. (org-element-map (org-element-contents report) 'headline
  608. (lambda (hl) (org-taskjuggler--build-report hl info))
  609. info nil 'headline)
  610. ""))
  611. ;; Closing report.
  612. "}\n"))
  613. (defun org-taskjuggler--build-task (task info)
  614. "Return a task declaration.
  615. TASK is a headline. INFO is a plist used as a communication
  616. channel.
  617. All valid attributes from TASK are inserted. If TASK defines
  618. a property \"task_id\" it will be used as the id for this task.
  619. Otherwise it will use the ID property. If neither is defined
  620. a unique id will be associated to it."
  621. (let* ((allocate (org-element-property :ALLOCATE task))
  622. (complete
  623. (if (eq (org-element-property :todo-type task) 'done) "100"
  624. (org-element-property :COMPLETE task)))
  625. (depends (org-taskjuggler-resolve-dependencies task info))
  626. (effort (org-element-property :EFFORT task))
  627. (milestone
  628. (or (org-element-property :MILESTONE task)
  629. (not (or (org-element-map (org-element-contents task) 'headline
  630. 'identity info t) ; Has task any child?
  631. effort
  632. (org-element-property :LENGTH task)
  633. (org-element-property :DURATION task)
  634. (and (org-taskjuggler-get-start task)
  635. (org-taskjuggler-get-end task))
  636. (org-element-property :PERIOD task)))))
  637. (priority
  638. (let ((pri (org-element-property :priority task)))
  639. (and pri
  640. (max 1 (/ (* 1000 (- org-lowest-priority pri))
  641. (- org-lowest-priority org-highest-priority)))))))
  642. (concat
  643. ;; Opening task.
  644. (format "task %s \"%s\" {\n"
  645. (org-taskjuggler-get-id task info)
  646. (org-taskjuggler-get-name task))
  647. ;; Add default attributes.
  648. (and depends
  649. (format " depends %s\n"
  650. (org-taskjuggler-format-dependencies depends task info)))
  651. (and allocate
  652. (format " purge %s\n allocate %s\n"
  653. ;; Compatibility for previous TaskJuggler versions.
  654. (if (>= org-taskjuggler-target-version 3.0) "allocate"
  655. "allocations")
  656. allocate))
  657. (and complete (format " complete %s\n" comptete))
  658. (and effort
  659. (format " effort %s\n"
  660. (let* ((minutes (org-duration-string-to-minutes effort))
  661. (hours (/ minutes 60.0)))
  662. (format "%.1fh" hours))))
  663. (and priority (format " priority %s\n" complete))
  664. (and milestone " milestone\n")
  665. ;; Add other valid attributes.
  666. (org-taskjuggler--indent-string
  667. (org-taskjuggler--build-attributes
  668. task org-taskjuggler-valid-task-attributes))
  669. ;; Add inner tasks.
  670. (org-taskjuggler--indent-string
  671. (mapconcat 'identity
  672. (org-element-map (org-element-contents task) 'headline
  673. (lambda (hl) (org-taskjuggler--build-task hl info))
  674. info nil 'headline)
  675. ""))
  676. ;; Closing task.
  677. "}\n")))
  678. ;;; Interactive Functions
  679. ;;;###autoload
  680. (defun org-taskjuggler-export (&optional async subtreep visible-only)
  681. "Export current buffer to a TaskJuggler file.
  682. The exporter looks for a tree with tag that matches
  683. `org-taskjuggler-project-tag' and takes this as the tasks for
  684. this project. The first node of this tree defines the project
  685. properties such as project name and project period.
  686. If there is a tree with tag that matches
  687. `org-taskjuggler-resource-tag' this tree is taken as resources
  688. for the project. If no resources are specified, a default
  689. resource is created and allocated to the project.
  690. Also the TaskJuggler project will be created with default reports
  691. as defined in `org-taskjuggler-default-reports'.
  692. If narrowing is active in the current buffer, only export its
  693. narrowed part.
  694. If a region is active, export that region.
  695. A non-nil optional argument ASYNC means the process should happen
  696. asynchronously. The resulting file should be accessible through
  697. the `org-export-stack' interface.
  698. When optional argument SUBTREEP is non-nil, export the sub-tree
  699. at point, extracting information from the headline properties
  700. first.
  701. When optional argument VISIBLE-ONLY is non-nil, don't export
  702. contents of hidden elements.
  703. Return output file's name."
  704. (interactive)
  705. (let ((outfile
  706. (org-export-output-file-name org-taskjuggler-extension subtreep)))
  707. (if async
  708. (org-export-async-start
  709. (lambda (f)
  710. (org-export-add-to-stack f 'taskjuggler)
  711. (run-hook-with-args 'org-taskjuggler-final-hook f))
  712. `(expand-file-name
  713. (org-export-to-file 'taskjuggler ,outfile ,subtreep ,visible-only)))
  714. (org-export-to-file 'taskjuggler outfile subtreep visible-only)
  715. (run-hook-with-args 'org-taskjuggler-final-hook outfile)
  716. outfile)))
  717. ;;;###autoload
  718. (defun org-taskjuggler-export-and-open (&optional subtreep visible-only)
  719. "Export current buffer to a TaskJuggler file and open it.
  720. The exporter looks for a tree with tag that matches
  721. `org-taskjuggler-project-tag' and takes this as the tasks for
  722. this project. The first node of this tree defines the project
  723. properties such as project name and project period.
  724. If there is a tree with tag that matches
  725. `org-taskjuggler-resource-tag' this tree is taken as resources
  726. for the project. If no resources are specified, a default
  727. resource is created and allocated to the project.
  728. Also the TaskJuggler project will be created with default reports
  729. as defined in `org-taskjuggler-default-reports'.
  730. If narrowing is active in the current buffer, only export its
  731. narrowed part.
  732. If a region is active, export that region.
  733. When optional argument SUBTREEP is non-nil, export the sub-tree
  734. at point, extracting information from the headline properties
  735. first.
  736. When optional argument VISIBLE-ONLY is non-nil, don't export
  737. contents of hidden elements.
  738. Open file with the TaskJuggler GUI."
  739. (interactive)
  740. (let* ((file (org-taskjuggler-export nil subtreep visible-only))
  741. (process-name "TaskJugglerUI")
  742. (command (concat process-name " " file)))
  743. (start-process-shell-command process-name nil command)))
  744. (provide 'ox-taskjuggler)
  745. ;; Local variables:
  746. ;; sentence-end-double-space: t
  747. ;; End:
  748. ;;; ox-taskjuggler.el ends here