ox-taskjuggler.el 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  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. ;; - Add support for org-export-with-planning
  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. By default a project plan is exported which conforms to version
  174. 3.x of TaskJuggler. For a project plan that is compatible with
  175. versions of TaskJuggler older than 3.0 set this to 2.4.
  176. If you change this variable be sure to also change
  177. `org-taskjuggler-default-reports' as the format of reports has
  178. changed considerably between version 2.x and 3.x of TaskJuggler"
  179. :group 'org-export-taskjuggler
  180. :type 'number)
  181. (defcustom org-taskjuggler-default-project-version "1.0"
  182. "Default version string for the project.
  183. This value can also be set with the \":VERSION:\" property
  184. associated to the headline defining the project."
  185. :group 'org-export-taskjuggler
  186. :type 'string)
  187. (defcustom org-taskjuggler-default-project-duration 280
  188. "Default project duration.
  189. The value will be used if no start and end date have been defined
  190. in the root node of the task tree, i.e. the tree that has been
  191. marked with `org-taskjuggler-project-tag'"
  192. :group 'org-export-taskjuggler
  193. :type 'integer)
  194. (defcustom org-taskjuggler-default-reports
  195. '("textreport report \"Plan\" {
  196. formats html
  197. header '== %title =='
  198. center -8<-
  199. [#Plan Plan] | [#Resource_Allocation Resource Allocation]
  200. ----
  201. === Plan ===
  202. <[report id=\"plan\"]>
  203. ----
  204. === Resource Allocation ===
  205. <[report id=\"resourceGraph\"]>
  206. ->8-
  207. }
  208. # A traditional Gantt chart with a project overview.
  209. taskreport plan \"\" {
  210. headline \"Project Plan\"
  211. columns bsi, name, start, end, effort, chart
  212. loadunit shortauto
  213. hideresource 1
  214. }
  215. # A graph showing resource allocation. It identifies whether each
  216. # resource is under- or over-allocated for.
  217. resourcereport resourceGraph \"\" {
  218. headline \"Resource Allocation Graph\"
  219. columns no, name, effort, weekly
  220. loadunit shortauto
  221. hidetask ~(isleaf() & isleaf_())
  222. sorttasks plan.start.up
  223. }")
  224. "Default reports for the project.
  225. These are sensible default reports to give a good out-of-the-box
  226. result when exporting without defining any reports. \"%title\"
  227. anywhere in the reports will be replaced with the document title.
  228. If you want to define your own reports you can change them here
  229. or simply define the default reports so that they include an
  230. external report definition as follows:
  231. include reports.tji
  232. These default are made to work with tj3. If you are targeting
  233. TaskJuggler 2.4 (see `org-taskjuggler-target-version') please
  234. change these defaults to something like the following:
  235. taskreport \"Gantt Chart\" {
  236. headline \"Project Gantt Chart\"
  237. columns hierarchindex, name, start, end, effort, duration, completed, chart
  238. timeformat \"%Y-%m-%d\"
  239. hideresource 1
  240. loadunit shortauto
  241. }
  242. resourcereport \"Resource Graph\" {
  243. headline \"Resource Allocation Graph\"
  244. columns no, name, utilization, freeload, chart
  245. loadunit shortauto
  246. sorttasks startup
  247. hidetask ~isleaf()
  248. }"
  249. :group 'org-export-taskjuggler
  250. :type '(repeat (string :tag "Report")))
  251. (defcustom org-taskjuggler-default-global-header ""
  252. "Default global header for the project.
  253. This goes before project declaration, and might be useful for
  254. early macros."
  255. :group 'org-export-taskjuggler
  256. :type '(string :tag "Preamble"))
  257. (defcustom org-taskjuggler-default-global-properties
  258. "shift s40 \"Part time shift\" {
  259. workinghours wed, thu, fri off
  260. }
  261. "
  262. "Default global properties for the project.
  263. Here you typically define global properties such as shifts,
  264. accounts, rates, vacation, macros and flags. Any property that
  265. is allowed within the TaskJuggler file can be inserted. You
  266. could for example include another TaskJuggler file.
  267. The global properties are inserted after the project declaration
  268. but before any resource and task declarations."
  269. :group 'org-export-taskjuggler
  270. :type '(string :tag "Preamble"))
  271. (defcustom org-taskjuggler-valid-task-attributes
  272. '(account start note duration endbuffer endcredit end
  273. flags journalentry length limits maxend maxstart minend
  274. minstart period reference responsible scheduling
  275. startbuffer startcredit statusnote chargeset charge)
  276. "Valid attributes for Taskjuggler tasks.
  277. If one of these appears as a property for a headline, it will be
  278. exported with the corresponding task."
  279. :group 'org-export-taskjuggler)
  280. (defcustom org-taskjuggler-valid-resource-attributes
  281. '(limits vacation shift booking efficiency journalentry rate
  282. workinghours flags)
  283. "Valid attributes for Taskjuggler resources.
  284. If one of these appears as a property for a headline, it will be
  285. exported with the corresponding resource."
  286. :group 'org-export-taskjuggler)
  287. (defcustom org-taskjuggler-valid-report-attributes
  288. '(headline columns definitions timeformat hideresource hidetask
  289. loadunit sorttasks formats period)
  290. "Valid attributes for Taskjuggler reports.
  291. If one of these appears as a property for a headline, it will be
  292. exported with the corresponding report."
  293. :group 'org-export-taskjuggler)
  294. (defcustom org-taskjuggler-process-command
  295. "tj3 --silent --no-color --output-dir %o %f"
  296. "Command to process a Taskjuggler file.
  297. The command will be given to the shell as a command to process a
  298. Taskjuggler file. \"%f\" in the command will be replaced by the
  299. full file name, \"%o\" by the reports directory (see
  300. `org-taskjuggler-reports-directory').
  301. If you are targeting Taskjuggler 2.4 (see
  302. `org-taskjuggler-target-version') this setting is ignored."
  303. :group 'org-export-taskjuggler)
  304. (defcustom org-taskjuggler-reports-directory "reports"
  305. "Default directory to generate the Taskjuggler reports in.
  306. The command `org-taskjuggler-process-command' generates the
  307. reports and associated files such as CSS inside this directory.
  308. If the directory is not an absolute path it is relative to the
  309. directory of the exported file. The directory is created if it
  310. doesn't exist.
  311. If you are targeting Taskjuggler 2.4 (see
  312. `org-taskjuggler-target-version') this setting is ignored."
  313. :group 'org-export-taskjuggler)
  314. (defcustom org-taskjuggler-keep-project-as-task t
  315. "Non-nil keeps the project headline as an umbrella task for all tasks.
  316. Setting this to nil will allow maintaining completely separated
  317. task buckets, while still sharing the same resources pool."
  318. :group 'org-export-taskjuggler
  319. :type 'boolean)
  320. ;;; Hooks
  321. (defvar org-taskjuggler-final-hook nil
  322. "Hook run after a TaskJuggler files has been saved.
  323. This hook is run with the name of the file as argument.")
  324. ;;; Back-End Definition
  325. (org-export-define-backend 'taskjuggler
  326. '((template . org-taskjuggler-project-plan))
  327. :menu-entry
  328. '(?J "Export to TaskJuggler"
  329. ((?j "As TJP file" (lambda (a s v b) (org-taskjuggler-export a s v)))
  330. (?p "As TJP file and process"
  331. (lambda (a s v b)
  332. (if a (org-taskjuggler-export a s v)
  333. (org-taskjuggler-export-and-process s v))))
  334. (?o "As TJP file, process and open"
  335. (lambda (a s v b)
  336. (if a (org-taskjuggler-export a s v)
  337. (org-taskjuggler-export-process-and-open s v))))))
  338. ;; This property will be used to store unique ids in communication
  339. ;; channel. Ids will be retrieved with `org-taskjuggler-get-id'.
  340. :options-alist '((:taskjuggler-unique-ids nil nil nil)))
  341. ;;; Unique IDs
  342. (defun org-taskjuggler-assign-task-ids (tasks info)
  343. "Assign a unique ID to each task in TASKS.
  344. TASKS is a list of headlines. INFO is a plist used as a
  345. communication channel. Return value is an alist between
  346. headlines and their associated ID. IDs are hierarchical, which
  347. means they only need to be unique among the task siblings."
  348. (let* (alist
  349. build-id ; For byte-compiler.
  350. (build-id
  351. (lambda (tasks local-ids)
  352. (org-element-map tasks 'headline
  353. (lambda (task)
  354. (let ((id (org-taskjuggler--build-unique-id task local-ids)))
  355. (push id local-ids)
  356. (push (cons task id) alist)
  357. (funcall build-id (org-element-contents task) nil)))
  358. info nil 'headline))))
  359. (funcall build-id tasks nil)
  360. alist))
  361. (defun org-taskjuggler-assign-resource-ids (resources info)
  362. "Assign a unique ID to each resource within RESOURCES.
  363. RESOURCES is a list of headlines. INFO is a plist used as a
  364. communication channel. Return value is an alist between
  365. headlines and their associated ID."
  366. (let (ids)
  367. (org-element-map resources 'headline
  368. (lambda (resource)
  369. (let ((id (org-taskjuggler--build-unique-id resource ids)))
  370. (push id ids)
  371. (cons resource id)))
  372. info)))
  373. ;;; Accessors
  374. (defun org-taskjuggler-get-project (info)
  375. "Return project in parse tree.
  376. INFO is a plist used as a communication channel. First headline
  377. in buffer with `org-taskjuggler-project-tag' defines the project.
  378. If no such task is defined, pick the first headline in buffer.
  379. If there is no headline at all, return nil."
  380. (let ((tree (plist-get info :parse-tree)))
  381. (or (org-element-map tree 'headline
  382. (lambda (hl)
  383. (and (member org-taskjuggler-project-tag
  384. (org-export-get-tags hl info))
  385. hl))
  386. info t)
  387. (org-element-map tree 'headline 'identity info t))))
  388. (defun org-taskjuggler-get-id (item info)
  389. "Return id for task or resource ITEM.
  390. ITEM is a headline. INFO is a plist used as a communication
  391. channel. Return value is a string."
  392. (cdr (assq item (plist-get info :taskjuggler-unique-ids))))
  393. (defun org-taskjuggler-get-name (item)
  394. "Return name for task or resource ITEM.
  395. ITEM is a headline. Return value is a string."
  396. ;; Quote double quotes in name.
  397. (replace-regexp-in-string
  398. "\"" "\\\"" (org-element-property :raw-value item) t t))
  399. (defun org-taskjuggler-get-start (item)
  400. "Return start date for task or resource ITEM.
  401. ITEM is a headline. Return value is a string or nil if ITEM
  402. doesn't have any start date defined."
  403. (let ((scheduled (org-element-property :scheduled item)))
  404. (or
  405. (and scheduled (org-timestamp-format scheduled "%Y-%02m-%02d"))
  406. (and (memq 'start org-taskjuggler-valid-task-attributes)
  407. (org-element-property :START item)))))
  408. (defun org-taskjuggler-get-end (item)
  409. "Return end date for task or resource ITEM.
  410. ITEM is a headline. Return value is a string or nil if ITEM
  411. doesn't have any end date defined."
  412. (let ((deadline (org-element-property :deadline item)))
  413. (and deadline (org-timestamp-format deadline "%Y-%02m-%02d"))))
  414. ;;; Internal Functions
  415. (defun org-taskjuggler--indent-string (s)
  416. "Indent string S by 2 spaces.
  417. Return new string. If S is the empty string, return it."
  418. (if (equal "" s) s (replace-regexp-in-string "^ *\\S-" " \\&" s)))
  419. (defun org-taskjuggler--build-attributes (item attributes)
  420. "Return attributes string for task, resource or report ITEM.
  421. ITEM is a headline. ATTRIBUTES is a list of symbols
  422. representing valid attributes for ITEM."
  423. (mapconcat
  424. (lambda (attribute)
  425. (let ((value (org-element-property
  426. (intern (upcase (format ":%s" attribute)))
  427. item)))
  428. (and value (format "%s %s\n" attribute value))))
  429. (remq nil attributes) ""))
  430. (defun org-taskjuggler--build-unique-id (item unique-ids)
  431. "Return a unique id for a given task or a resource.
  432. ITEM is an `headline' type element representing the task or
  433. resource. Its id is derived from its name and made unique
  434. against UNIQUE-IDS. If the (downcased) first token of the
  435. headline is not unique try to add more (downcased) tokens of the
  436. headline or finally add more underscore characters (\"_\")."
  437. (let ((id (org-string-nw-p (org-element-property :TASK_ID item))))
  438. ;; If an id is specified, use it, as long as it's unique.
  439. (if (and id (not (member id unique-ids))) id
  440. (let* ((parts (org-split-string (org-element-property :raw-value item)))
  441. (id (org-taskjuggler--clean-id (downcase (pop parts)))))
  442. ;; Try to add more parts of the headline to make it unique.
  443. (while (and (car parts) (member id unique-ids))
  444. (setq id (concat id "_"
  445. (org-taskjuggler--clean-id (downcase (pop parts))))))
  446. ;; If it's still not unique, add "_".
  447. (while (member id unique-ids)
  448. (setq id (concat id "_")))
  449. id))))
  450. (defun org-taskjuggler--clean-id (id)
  451. "Clean and return ID to make it acceptable for TaskJuggler.
  452. ID is a string."
  453. ;; Replace non-ascii by "_".
  454. (replace-regexp-in-string
  455. "[^a-zA-Z0-9_]" "_"
  456. ;; Make sure id doesn't start with a number.
  457. (replace-regexp-in-string "^\\([0-9]\\)" "_\\1" id)))
  458. ;;; Dependencies
  459. (defun org-taskjuggler-resolve-dependencies (task info)
  460. "Return a list of all tasks TASK depends on.
  461. TASK is a headline. INFO is a plist used as a communication
  462. channel."
  463. (let ((deps-ids
  464. ;; Get all dependencies specified in BLOCKER and DEPENDS task
  465. ;; properties. Clean options from them.
  466. (let ((deps (concat (org-element-property :BLOCKER task)
  467. (org-element-property :DEPENDS task))))
  468. (and deps
  469. (org-split-string (replace-regexp-in-string "{.*?}" "" deps)
  470. "[ ,]* +"))))
  471. depends)
  472. (when deps-ids
  473. ;; Find tasks with :task_id: property matching id in DEPS-IDS.
  474. ;; Add them to DEPENDS.
  475. (let* ((project (org-taskjuggler-get-project info))
  476. (tasks (if org-taskjuggler-keep-project-as-task project
  477. (org-element-contents project))))
  478. (setq depends
  479. (org-element-map tasks 'headline
  480. (lambda (task)
  481. (let ((task-id (or (org-element-property :TASK_ID task)
  482. (org-element-property :ID task))))
  483. (and task-id (member task-id deps-ids) task)))
  484. info)))
  485. ;; Check BLOCKER and DEPENDS properties. If "previous-sibling"
  486. ;; belongs to DEPS-ID, add it to DEPENDS.
  487. (when (and (member-ignore-case "previous-sibling" deps-ids)
  488. (not (org-export-first-sibling-p task info)))
  489. (let ((prev (org-export-get-previous-element task info)))
  490. (and (not (memq prev depends)) (push prev depends)))))
  491. ;; Check ORDERED status of parent.
  492. (let ((parent (org-export-get-parent task)))
  493. (when (and parent
  494. (org-element-property :ORDERED parent)
  495. (not (org-export-first-sibling-p task info)))
  496. (push (org-export-get-previous-element task info) depends)))
  497. ;; Return dependencies.
  498. depends))
  499. (defun org-taskjuggler-format-dependencies (dependencies task info)
  500. "Format DEPENDENCIES to match TaskJuggler syntax.
  501. DEPENDENCIES is list of dependencies for TASK, as returned by
  502. `org-taskjuggler-resolve-depedencies'. TASK is a headline.
  503. INFO is a plist used as a communication channel. Return value
  504. doesn't include leading \"depends\"."
  505. (let* ((dep-str (concat (org-element-property :BLOCKER task)
  506. " "
  507. (org-element-property :DEPENDS task)))
  508. (get-path
  509. (lambda (dep)
  510. ;; Return path to DEP relatively to TASK.
  511. (let ((parent (org-export-get-parent task))
  512. (exclamations 1)
  513. (option
  514. (let ((id (org-element-property :TASK_ID dep)))
  515. (and id
  516. (string-match (concat id " +\\({.*?}\\)") dep-str)
  517. (org-match-string-no-properties 1))))
  518. path)
  519. ;; Compute number of exclamation marks by looking for the
  520. ;; common ancestor between TASK and DEP.
  521. (while (not (org-element-map parent 'headline
  522. (lambda (hl) (eq hl dep))))
  523. (incf exclamations)
  524. (setq parent (org-export-get-parent parent)))
  525. ;; Build path from DEP to PARENT.
  526. (while (not (eq parent dep))
  527. (push (org-taskjuggler-get-id dep info) path)
  528. (setq dep (org-export-get-parent dep)))
  529. ;; Return full path. Add dependency options, if any.
  530. (concat (make-string exclamations ?!)
  531. (mapconcat 'identity path ".")
  532. (and option (concat " " option)))))))
  533. ;; Return dependencies string, without the leading "depends".
  534. (mapconcat (lambda (dep) (funcall get-path dep)) dependencies ", ")))
  535. ;;; Translator Functions
  536. (defun org-taskjuggler-project-plan (contents info)
  537. "Build TaskJuggler project plan.
  538. CONTENTS is ignored. INFO is a plist holding export options.
  539. Return complete project plan as a string in TaskJuggler syntax."
  540. (let* ((tree (plist-get info :parse-tree))
  541. (project (or (org-taskjuggler-get-project info)
  542. (error "No project specified"))))
  543. (concat
  544. ;; 1. Insert header.
  545. (org-element-normalize-string org-taskjuggler-default-global-header)
  546. ;; 2. Insert project.
  547. (org-taskjuggler--build-project project info)
  548. ;; 3. Insert global properties.
  549. (org-element-normalize-string org-taskjuggler-default-global-properties)
  550. ;; 4. Insert resources. Provide a default one if none is
  551. ;; specified.
  552. (let ((main-resources
  553. ;; Collect contents from various trees marked with
  554. ;; `org-taskjuggler-resource-tag'. Only gather top level
  555. ;; resources.
  556. (apply 'append
  557. (org-element-map tree 'headline
  558. (lambda (hl)
  559. (and (member org-taskjuggler-resource-tag
  560. (org-export-get-tags hl info))
  561. (org-element-map (org-element-contents hl) 'headline
  562. 'identity info nil 'headline)))
  563. info nil 'headline))))
  564. ;; Assign a unique ID to each resource. Store it under
  565. ;; `:taskjuggler-unique-ids' property in INFO.
  566. (setq info
  567. (plist-put info :taskjuggler-unique-ids
  568. (org-taskjuggler-assign-resource-ids
  569. main-resources info)))
  570. (concat
  571. (if main-resources
  572. (mapconcat
  573. (lambda (resource) (org-taskjuggler--build-resource resource info))
  574. main-resources "")
  575. (format "resource %s \"%s\" {\n}\n" (user-login-name) user-full-name))
  576. ;; 5. Insert tasks.
  577. (let ((main-tasks
  578. ;; If `org-taskjuggler-keep-project-as-task' is
  579. ;; non-nil, there is only one task. Otherwise, every
  580. ;; direct children of PROJECT is a top level task.
  581. (if org-taskjuggler-keep-project-as-task (list project)
  582. (or (org-element-map (org-element-contents project) 'headline
  583. 'identity info nil 'headline)
  584. (error "No task specified")))))
  585. ;; Assign a unique ID to each task. Add it to
  586. ;; `:taskjuggler-unique-ids' property in INFO.
  587. (setq info
  588. (plist-put info :taskjuggler-unique-ids
  589. (append
  590. (org-taskjuggler-assign-task-ids main-tasks info)
  591. (plist-get info :taskjuggler-unique-ids))))
  592. ;; If no resource is allocated among tasks, allocate one to
  593. ;; the first task.
  594. (unless (org-element-map main-tasks 'headline
  595. (lambda (task) (org-element-property :ALLOCATE task))
  596. info t)
  597. (org-element-put-property
  598. (car main-tasks) :ALLOCATE
  599. (or (org-taskjuggler-get-id (car main-resources) info)
  600. (user-login-name))))
  601. (mapconcat
  602. (lambda (task) (org-taskjuggler--build-task task info))
  603. main-tasks ""))
  604. ;; 6. Insert reports. If no report is defined, insert default
  605. ;; reports.
  606. (let ((main-reports
  607. ;; Collect contents from various trees marked with
  608. ;; `org-taskjuggler-report-tag'. Only gather top level
  609. ;; reports.
  610. (apply 'append
  611. (org-element-map tree 'headline
  612. (lambda (hl)
  613. (and (member org-taskjuggler-report-tag
  614. (org-export-get-tags hl info))
  615. (org-element-map (org-element-contents hl)
  616. 'headline 'identity info nil 'headline)))
  617. info nil 'headline))))
  618. (if main-reports
  619. (mapconcat
  620. (lambda (report) (org-taskjuggler--build-report report info))
  621. main-reports "")
  622. ;; insert title in default reports
  623. (let* ((title (org-export-data (plist-get info :title) info))
  624. (report-title (if (string= title "")
  625. (org-taskjuggler-get-name project)
  626. title)))
  627. (mapconcat
  628. 'org-element-normalize-string
  629. (mapcar
  630. (function
  631. (lambda (report)
  632. (replace-regexp-in-string "%title" report-title report t t)))
  633. org-taskjuggler-default-reports) "")))))))))
  634. (defun org-taskjuggler--build-project (project info)
  635. "Return a project declaration.
  636. PROJECT is a headline. INFO is a plist used as a communication
  637. channel. If no start date is specified, start today. If no end
  638. date is specified, end `org-taskjuggler-default-project-duration'
  639. days from now."
  640. (format "project %s \"%s\" \"%s\" %s %s {\n}\n"
  641. (org-taskjuggler-get-id project info)
  642. (org-taskjuggler-get-name project)
  643. ;; Version is obtained through :TASKJUGGLER_VERSION:
  644. ;; property or `org-taskjuggler-default-project-version'.
  645. (or (org-element-property :VERSION project)
  646. org-taskjuggler-default-project-version)
  647. (or (org-taskjuggler-get-start project)
  648. (format-time-string "%Y-%m-%d"))
  649. (let ((end (org-taskjuggler-get-end project)))
  650. (or (and end (format "- %s" end))
  651. (format "+%sd" org-taskjuggler-default-project-duration)))))
  652. (defun org-taskjuggler--build-resource (resource info)
  653. "Return a resource declaration.
  654. RESOURCE is a headline. INFO is a plist used as a communication
  655. channel.
  656. All valid attributes from RESOURCE are inserted. If RESOURCE
  657. defines a property \"resource_id\" it will be used as the id for
  658. this resource. Otherwise it will use the ID property. If
  659. neither is defined a unique id will be associated to it."
  660. (concat
  661. ;; Opening resource.
  662. (format "resource %s \"%s\" {\n"
  663. (org-taskjuggler--clean-id
  664. (or (org-element-property :RESOURCE_ID resource)
  665. (org-element-property :ID resource)
  666. (org-taskjuggler-get-id resource info)))
  667. (org-taskjuggler-get-name resource))
  668. ;; Add attributes.
  669. (org-taskjuggler--indent-string
  670. (org-taskjuggler--build-attributes
  671. resource org-taskjuggler-valid-resource-attributes))
  672. ;; Add inner resources.
  673. (org-taskjuggler--indent-string
  674. (mapconcat
  675. 'identity
  676. (org-element-map (org-element-contents resource) 'headline
  677. (lambda (hl) (org-taskjuggler--build-resource hl info))
  678. info nil 'headline)
  679. ""))
  680. ;; Closing resource.
  681. "}\n"))
  682. (defun org-taskjuggler--build-report (report info)
  683. "Return a report declaration.
  684. REPORT is a headline. INFO is a plist used as a communication
  685. channel."
  686. (concat
  687. ;; Opening report.
  688. (format "%s \"%s\" {\n"
  689. (or (org-element-property :REPORT_KIND report) "taskreport")
  690. (org-taskjuggler-get-name report))
  691. ;; Add attributes.
  692. (org-taskjuggler--indent-string
  693. (org-taskjuggler--build-attributes
  694. report org-taskjuggler-valid-report-attributes))
  695. ;; Add inner reports.
  696. (org-taskjuggler--indent-string
  697. (mapconcat
  698. 'identity
  699. (org-element-map (org-element-contents report) 'headline
  700. (lambda (hl) (org-taskjuggler--build-report hl info))
  701. info nil 'headline)
  702. ""))
  703. ;; Closing report.
  704. "}\n"))
  705. (defun org-taskjuggler--build-task (task info)
  706. "Return a task declaration.
  707. TASK is a headline. INFO is a plist used as a communication
  708. channel.
  709. All valid attributes from TASK are inserted. If TASK defines
  710. a property \"task_id\" it will be used as the id for this task.
  711. Otherwise it will use the ID property. If neither is defined
  712. a unique id will be associated to it."
  713. (let* ((allocate (org-element-property :ALLOCATE task))
  714. (complete
  715. (if (eq (org-element-property :todo-type task) 'done) "100"
  716. (org-element-property :COMPLETE task)))
  717. (depends (org-taskjuggler-resolve-dependencies task info))
  718. (effort (let ((property
  719. (intern (concat ":" (upcase org-effort-property)))))
  720. (org-element-property property task)))
  721. (milestone
  722. (or (org-element-property :MILESTONE task)
  723. (not (or (org-element-map (org-element-contents task) 'headline
  724. 'identity info t) ; Has task any child?
  725. effort
  726. (org-element-property :LENGTH task)
  727. (org-element-property :DURATION task)
  728. (and (org-taskjuggler-get-start task)
  729. (org-taskjuggler-get-end task))
  730. (org-element-property :PERIOD task)))))
  731. (priority
  732. (let ((pri (org-element-property :priority task)))
  733. (and pri
  734. (max 1 (/ (* 1000 (- org-lowest-priority pri))
  735. (- org-lowest-priority org-highest-priority)))))))
  736. (concat
  737. ;; Opening task.
  738. (format "task %s \"%s\" {\n"
  739. (org-taskjuggler-get-id task info)
  740. (org-taskjuggler-get-name task))
  741. ;; Add default attributes.
  742. (and depends
  743. (format " depends %s\n"
  744. (org-taskjuggler-format-dependencies depends task info)))
  745. (and allocate
  746. (format " purge %s\n allocate %s\n"
  747. ;; Compatibility for previous TaskJuggler versions.
  748. (if (>= org-taskjuggler-target-version 3.0) "allocate"
  749. "allocations")
  750. allocate))
  751. (and complete (format " complete %s\n" complete))
  752. (and effort
  753. (format " effort %s\n"
  754. (let* ((minutes (org-duration-string-to-minutes effort))
  755. (hours (/ minutes 60.0)))
  756. (format "%.1fh" hours))))
  757. (and priority (format " priority %s\n" priority))
  758. (and milestone " milestone\n")
  759. ;; Add other valid attributes.
  760. (org-taskjuggler--indent-string
  761. (org-taskjuggler--build-attributes
  762. task org-taskjuggler-valid-task-attributes))
  763. ;; Add inner tasks.
  764. (org-taskjuggler--indent-string
  765. (mapconcat 'identity
  766. (org-element-map (org-element-contents task) 'headline
  767. (lambda (hl) (org-taskjuggler--build-task hl info))
  768. info nil 'headline)
  769. ""))
  770. ;; Closing task.
  771. "}\n")))
  772. ;;; Interactive Functions
  773. ;;;###autoload
  774. (defun org-taskjuggler-export (&optional async subtreep visible-only)
  775. "Export current buffer to a TaskJuggler file.
  776. The exporter looks for a tree with tag that matches
  777. `org-taskjuggler-project-tag' and takes this as the tasks for
  778. this project. The first node of this tree defines the project
  779. properties such as project name and project period.
  780. If there is a tree with tag that matches
  781. `org-taskjuggler-resource-tag' this tree is taken as resources
  782. for the project. If no resources are specified, a default
  783. resource is created and allocated to the project.
  784. Also the TaskJuggler project will be created with default reports
  785. as defined in `org-taskjuggler-default-reports'.
  786. If narrowing is active in the current buffer, only export its
  787. narrowed part.
  788. If a region is active, export that region.
  789. A non-nil optional argument ASYNC means the process should happen
  790. asynchronously. The resulting file should be accessible through
  791. the `org-export-stack' interface.
  792. When optional argument SUBTREEP is non-nil, export the sub-tree
  793. at point, extracting information from the headline properties
  794. first.
  795. When optional argument VISIBLE-ONLY is non-nil, don't export
  796. contents of hidden elements.
  797. Return output file's name."
  798. (interactive)
  799. (let ((outfile
  800. (org-export-output-file-name org-taskjuggler-extension subtreep)))
  801. (org-export-to-file 'taskjuggler outfile
  802. async subtreep visible-only nil nil
  803. (lambda (file)
  804. (run-hook-with-args 'org-taskjuggler-final-hook file) nil))))
  805. ;;;###autoload
  806. (defun org-taskjuggler-export-and-process (&optional subtreep visible-only)
  807. "Export current buffer to a TaskJuggler file and process it.
  808. The exporter looks for a tree with tag that matches
  809. `org-taskjuggler-project-tag' and takes this as the tasks for
  810. this project. The first node of this tree defines the project
  811. properties such as project name and project period.
  812. If there is a tree with tag that matches
  813. `org-taskjuggler-resource-tag' this tree is taken as resources
  814. for the project. If no resources are specified, a default
  815. resource is created and allocated to the project.
  816. Also the TaskJuggler project will be created with default reports
  817. as defined in `org-taskjuggler-default-reports'.
  818. If narrowing is active in the current buffer, only export its
  819. narrowed part.
  820. If a region is active, export that region.
  821. When optional argument SUBTREEP is non-nil, export the sub-tree
  822. at point, extracting information from the headline properties
  823. first.
  824. When optional argument VISIBLE-ONLY is non-nil, don't export
  825. contents of hidden elements.
  826. Return a list of reports."
  827. (interactive)
  828. (let ((file (org-taskjuggler-export nil subtreep visible-only)))
  829. (org-taskjuggler-compile file)))
  830. ;;;###autoload
  831. (defun org-taskjuggler-export-process-and-open (&optional subtreep visible-only)
  832. "Export current buffer to a TaskJuggler file, process and open it.
  833. Export and process the file using
  834. `org-taskjuggler-export-and-process' and open the generated
  835. reports with a browser.
  836. If you are targeting TaskJuggler 2.4 (see
  837. `org-taskjuggler-target-version') the processing and display of
  838. the reports is done using the TaskJuggler GUI."
  839. (interactive)
  840. (if (< org-taskjuggler-target-version 3.0)
  841. (let* ((process-name "TaskJugglerUI")
  842. (command
  843. (concat process-name " "
  844. (org-taskjuggler-export nil subtreep visible-only))))
  845. (start-process-shell-command process-name nil command))
  846. (dolist (report (org-taskjuggler-export-and-process subtreep visible-only))
  847. (org-open-file report))))
  848. (defun org-taskjuggler-compile (file)
  849. "Compile a TaskJuggler file.
  850. FILE is the name of the file being compiled. Processing is done
  851. through the command given in `org-taskjuggler-process-command'.
  852. Return a list of reports."
  853. (let* ((full-name (file-truename file))
  854. (out-dir
  855. (expand-file-name
  856. org-taskjuggler-reports-directory (file-name-directory file)))
  857. errors)
  858. (message (format "Processing TaskJuggler file %s..." file))
  859. (save-window-excursion
  860. (let ((outbuf (get-buffer-create "*Org Taskjuggler Output*")))
  861. (unless (file-directory-p out-dir)
  862. (make-directory out-dir t))
  863. (with-current-buffer outbuf (erase-buffer))
  864. (shell-command
  865. (replace-regexp-in-string
  866. "%f" (shell-quote-argument full-name)
  867. (replace-regexp-in-string
  868. "%o" (shell-quote-argument out-dir)
  869. org-taskjuggler-process-command t t) t t) outbuf)
  870. ;; Collect standard errors from output buffer.
  871. (setq errors (org-taskjuggler--collect-errors outbuf)))
  872. (if (not errors)
  873. (message "Process completed.")
  874. (error (format "TaskJuggler failed with errors: %s" errors))))
  875. (file-expand-wildcards (format "%s/*.html" out-dir))))
  876. (defun org-taskjuggler--collect-errors (buffer)
  877. "Collect some kind of errors from \"tj3\" command output.
  878. BUFFER is the buffer containing output.
  879. Return collected error types as a string, or nil if there was
  880. none."
  881. (with-current-buffer buffer
  882. (save-excursion
  883. (goto-char (point-min))
  884. (let ((case-fold-search t)
  885. (errors ""))
  886. (while (re-search-forward "^.+:[0-9]+: \\(.*\\)$" nil t)
  887. (setq errors (concat errors " " (match-string 1))))
  888. (and (org-string-nw-p errors) (org-trim errors))))))
  889. (provide 'ox-taskjuggler)
  890. ;; Local variables:
  891. ;; sentence-end-double-space: t
  892. ;; End:
  893. ;;; ox-taskjuggler.el ends here