ox-taskjuggler.el 33 KB

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