summaryrefslogtreecommitdiff
path: root/IkiWiki/Setup/Standard.pm
blob: c8506930475a0c28ead9e613e7cde64d982b6db9 (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Setup::Standard;
  3. use warnings;
  4. use strict;
  5. use IkiWiki;
  6. # Parameters to import should be all the standard ikiwiki config, in a hash.
  7. sub import {
  8. IkiWiki::Setup::merge($_[1]);
  9. }
  10. sub gendump ($@) {
  11. my $class=shift;
  12. "#!/usr/bin/perl",
  13. "#",
  14. (map { "# $_" } @_),
  15. "use IkiWiki::Setup::Standard {",
  16. IkiWiki::Setup::commented_dump(\&dumpline, "\t"),
  17. "}"
  18. }
  19. sub dumpline ($$$$) {
  20. my $key=shift;
  21. my $value=shift;
  22. my $type=shift;
  23. my $prefix=shift;
  24. eval q{use Data::Dumper};
  25. error($@) if $@;
  26. local $Data::Dumper::Terse=1;
  27. local $Data::Dumper::Indent=1;
  28. local $Data::Dumper::Pad="\t";
  29. local $Data::Dumper::Sortkeys=1;
  30. local $Data::Dumper::Quotekeys=0;
  31. # only the perl version preserves utf-8 in output
  32. local $Data::Dumper::Useperl=1;
  33. my $dumpedvalue;
  34. if (($type eq 'boolean' || $type eq 'integer') && $value=~/^[0-9]+$/) {
  35. # avoid quotes
  36. $dumpedvalue=$value;
  37. }
  38. elsif (ref $value eq 'ARRAY' && @$value && ! grep { /[^\S]/ } @$value) {
  39. # dump simple array as qw{}
  40. $dumpedvalue="[qw{".join(" ", @$value)."}]";
  41. }
  42. else {
  43. $dumpedvalue=Dumper($value);
  44. chomp $dumpedvalue;
  45. if (length $prefix) {
  46. # add to second and subsequent lines
  47. my @lines=split(/\n/, $dumpedvalue);
  48. $dumpedvalue="";
  49. for (my $x=0; $x <= $#lines; $x++) {
  50. $lines[$x] =~ s/^\t//;
  51. $dumpedvalue.="\t".($x ? $prefix : "").$lines[$x]."\n";
  52. }
  53. }
  54. $dumpedvalue=~s/^\t//;
  55. chomp $dumpedvalue;
  56. }
  57. return "\t$prefix$key => $dumpedvalue,";
  58. }
  59. 1