tudent project to explore semantic web for learning aid
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';