ox-taskjuggler.el 33 KB

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