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