summaryrefslogtreecommitdiff
path: root/bin/hedgedoc2quarto
blob: 5e2eb70b01dfad714be1d29602ef14cb46716164 (plain)
  1. #!/usr/bin/perl
  2. use v5.36;
  3. use strict;
  4. use utf8;
  5. =head1 NAME
  6. hedgedoc2quarto - convert HedgeDoc content to Quarto
  7. =head1 VERSION
  8. Version 0.0.1
  9. =head1 SYNOPSIS
  10. hedgedoc2quarto INFILE OUTFILE
  11. hedgedoc2quarto < INFILE > OUTFILE
  12. =head1 DESCRIPTION
  13. B<hedgedoc2quarto> reformats text content
  14. from HedgeDoc- to Quarto-flavored Markdown,
  15. and adapts embedded diagram code.
  16. Both HedgeDoc and Quarto uses Markdown,
  17. but different flavors,
  18. and whereas both handle (different subsets of) Mermaid diagrams,
  19. Quarto also (through plugins) handles PlantUML diagrams.
  20. =cut
  21. # slurp INFILE if passed as first argument, or else STDIN
  22. my ( $infile, $outfile, $bogus ) = @ARGV;
  23. die 'Too many arguments: expected INFILE and OUTFILE' if $bogus;
  24. @ARGV = ($infile) if $infile;
  25. my $content = do { local $/ = undef; <> };
  26. # TODO: integrate with subroutine handler below
  27. $content =~ s/^
  28. (?'fence'[``~]{3,})\s*
  29. \Kgraphviz\n
  30. (?'code'.*?\n)
  31. \k'fence'
  32. $/{dot}\n\/\/| fig-width: 100\%\n$+{code}$+{fence}/gsmx;
  33. $content =~ s/^
  34. (?'fence'[``~]{3,})\s*
  35. \Kmermaid\n
  36. (?'type'gantt)\n
  37. (?'code'.*?\n)
  38. \k'fence'
  39. $/
  40. # FIXME: implement option to choose output diagram language
  41. # "{mermaid}\n\%\%| fig-width: 100\%\n"
  42. # . &mmd2mmd( $+{type}, $+{code} )
  43. "{.plantuml}\n\%\%| fig-width: 100\%\n"
  44. . &mmd2puml( $+{type}, $+{code} )
  45. . $+{fence}
  46. /gsmex;
  47. if ($outfile) {
  48. open( FH, '>', $outfile ) or die $!;
  49. print FH $content;
  50. }
  51. else {
  52. print $content;
  53. }
  54. sub mmd2mmd ( $type, $code )
  55. {
  56. # strip special comment marker '%%QUARTO%%'
  57. $code =~ s/^\s*+\K%%QUARTO%%//gm;
  58. return "$type\n$code";
  59. }
  60. sub mmd2puml ( $type, $code )
  61. {
  62. my @newcode;
  63. # strip special comment marker '%%QUARTO%%'
  64. $code =~ s/^\s*+\K%%QUARTO%%//gm;
  65. open my $fh, '<', \$code or die $!;
  66. while (<$fh>) {
  67. /^\s*+$/
  68. and push @newcode, ''
  69. and next;
  70. /^(\s*+)%%PLANTUML%%\K.*/
  71. and push @newcode, "$1$&"
  72. and next;
  73. # convert comments markers
  74. /^(\s*+)(?:[%]{2,}(?'comment'\s*+))?+\K.*/;
  75. my $indent = defined( $+{comment} ) ? "$1'$2" : $1;
  76. $_ = $&;
  77. /^title\s/i
  78. and push @newcode, "${indent}$_"
  79. and next;
  80. /^excludes\s+weekends\b/i
  81. and push @newcode, "${indent}saturday are closed"
  82. and push @newcode, "${indent}sunday are closed"
  83. and next;
  84. /^weekday\s+\K(?:mon|tues|wednes|thurs|fri|satur|sun)day\b/i
  85. and push @newcode, "${indent}weeks start on $&"
  86. and next;
  87. /^(?:date|axis)Format\s/i
  88. and push @newcode, "${indent}'UNSUPPORTED: $_"
  89. and next;
  90. /^todayMarker\s+(off|on)\b/i
  91. and push @newcode, "${indent}'UNSUPPORTED' $_"
  92. and next;
  93. /^section\s+\K\S+(?:\s+\S+)*/i
  94. and push @newcode, "${indent}-- $& --"
  95. and next;
  96. if (/^tickInterval\s+(?'tickAmount'\d+)(?'tickUnit'millisecond|second|minute|hour|day|week|month)\s*$/i
  97. )
  98. {
  99. push @newcode, "${indent}projectscale daily"
  100. and next
  101. if $+{tickAmount} eq 1
  102. and $+{tickUnit} eq 'day';
  103. push @newcode, "${indent}projectscale weekly" and next
  104. if $+{tickAmount} eq 1 and $+{tickUnit} eq 'week'
  105. or $+{tickAmount} eq 7 and $+{tickUnit} eq 'day';
  106. push @newcode, "${indent}projectscale monthly"
  107. and next
  108. if $+{tickAmount} eq 1
  109. and $+{tickUnit} eq 'month';
  110. push @newcode, "${indent}projectscale quarterly"
  111. and next
  112. if $+{tickAmount} eq 3
  113. and $+{tickUnit} eq 'month';
  114. push @newcode, "${indent}projectscale yearly"
  115. and next
  116. if $+{tickAmount} eq 12
  117. and $+{tickUnit} eq 'month';
  118. push @newcode, "${indent}'UNSUPPORTED' $&"
  119. and next;
  120. }
  121. /^
  122. (?'title'[^:\n]+)
  123. \s*+:\s*+
  124. # optional tags
  125. (?:
  126. (?:
  127. (?'active'active)
  128. |
  129. (?'done'done)
  130. |
  131. (?'crit'crit)
  132. |
  133. (?'milestone'milestone)
  134. )\s*+
  135. ,\s*+
  136. )?+
  137. (?:
  138. # optional tertiary item
  139. (?:
  140. (?'taskID'(?&id))\s*+
  141. ,\s*+
  142. (?=.*,) # several items must follow
  143. )?+
  144. # optional secondary item
  145. (?:
  146. (?'startDate'(?&date))
  147. |
  148. after
  149. (?'afterTaskIDs'
  150. (?:\s+(?&id))++
  151. )
  152. )\s*+
  153. ,\s*+
  154. )?+
  155. # required main item
  156. (?:
  157. (?'endDate'(?&date))
  158. |
  159. until
  160. (?'untilTaskIDs'
  161. (?:\s+(?&id))++
  162. )
  163. |
  164. (?'duration'\d+)
  165. \s*+d
  166. )\s*+
  167. (?(DEFINE)
  168. (?'id'[^\s\d,][^\s,]*+) # assume digit as lead caracter is illegal
  169. (?'date'\d\d\d\d(?:-\d\d(?:-\d\d)?+)?+)
  170. )
  171. $/x
  172. or defined( $+{comment} )
  173. and push @newcode, "${indent}$_"
  174. and next
  175. or die "unhandled syntax on line $.: $_";
  176. defined( $+{active} )
  177. or defined( $+{done} )
  178. or defined( $+{crit} )
  179. and die "unhandled tag on line $.: $_";
  180. my $task = "${indent}\[$+{title}]";
  181. my $taskref = $task;
  182. # optional 3rd item
  183. if ( $+{taskID} ) {
  184. $task .= " as [$+{taskID}]";
  185. $taskref = "${indent}\[$+{taskID}]";
  186. }
  187. if ( defined( $+{afterTaskIDs} ) ) {
  188. my @reqs = split ' ', $+{afterTaskIDs};
  189. if ( $+{milestone} ) {
  190. push @newcode, "$task happens at [$_]'s end" for @reqs;
  191. }
  192. elsif ( $+{endDate} ) {
  193. push @newcode, "$task ends $+{endDate}";
  194. push( @newcode, "$taskref starts at [$_]'s end" ) for @reqs;
  195. }
  196. elsif ( defined( $+{untilTaskIDs} ) ) {
  197. my @reqsEnd = split ' ', $+{untilTaskIDs};
  198. push @newcode, "$task ends at [$_]'s end" for @reqsEnd;
  199. push( @newcode, "$taskref starts at [$_]'s end" ) for @reqs;
  200. }
  201. else {
  202. push @newcode, "$task requires $+{duration} days";
  203. push( @newcode, "$taskref starts at [$_]'s end" ) for @reqs;
  204. }
  205. }
  206. else {
  207. if ( $+{milestone} ) {
  208. push @newcode, "$task happens $+{startDate}";
  209. }
  210. elsif ( $+{endDate} ) {
  211. push @newcode,
  212. "$task starts $+{startDate} and ends $+{ednDate}";
  213. }
  214. elsif ( defined( $+{untilTaskIDs} ) ) {
  215. my @reqsEnd = split ' ', $+{untilTaskIDs};
  216. push @newcode, "$task starts $+{startDate}";
  217. push @newcode, "$task ends at [$_]'s end" for @reqsEnd;
  218. }
  219. else {
  220. push @newcode,
  221. "$task starts $+{startDate} and requires $+{duration} days";
  222. }
  223. }
  224. }
  225. $" = "\n";
  226. return "\@start$type\n@newcode\n\@end$type\n";
  227. }
  228. =encoding UTF-8
  229. =head1 AUTHOR
  230. Jonas Smedegaard C<< <dr@jones.dk> >>
  231. =head1 COPYRIGHT AND LICENSE
  232. Copyright © 2024 Jonas Smedegaard
  233. This program is free software:
  234. you can redistribute it and/or modify it
  235. under the terms of the GNU Affero General Public License
  236. as published by the Free Software Foundation,
  237. either version 3, or (at your option) any later version.
  238. This program is distributed in the hope that it will be useful,
  239. but WITHOUT ANY WARRANTY;
  240. without even the implied warranty
  241. of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  242. See the GNU Affero General Public License for more details.
  243. You should have received a copy
  244. of the GNU Affero General Public License along with this program.
  245. If not, see <https://www.gnu.org/licenses/>.
  246. =cut
  247. 1;