« blib/lib/UR/DataSource/RDBMS/Operator/In.pm
↑ Summary
blib/lib/UR/Namespace/Command/Test/Window.pm »
| 1 | package UR::Doc::Writer::Html; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use UR; | ||
| 7 | our $VERSION = "0.46"; # UR $VERSION; | ||
| 8 | |||
| 9 | use UR::Doc::Section; | ||
| 10 | use UR::Doc::Pod2Html; | ||
| 11 | use Carp qw/croak/; | ||
| 12 | |||
| 13 | class UR::Doc::Writer::Html { | ||
| 14 | is => 'UR::Doc::Writer', | ||
| 15 | }; | ||
| 16 | |||
| 17 | sub render { | ||
| 18 | my $self = shift; | ||
| 19 | $self->content(''); | ||
| 20 | $self->_render_header; | ||
| 21 | $self->_render_index; | ||
| 22 | my $i = 0; | ||
| 23 | for my $section ($self->sections) { | ||
| 24 | $self->_render_section($section, $i++); | ||
| 25 | } | ||
| 26 | $self->_render_footer; | ||
| 27 | } | ||
| 28 | |||
| 29 | sub _render_header { | ||
| 30 | my $self = shift; | ||
| 31 | |||
| 32 | if ($self->navigation) { | ||
| 33 | my @nav_html; | ||
| 34 | for my $item (@{$self->navigation}) { | ||
| 35 | my ($name, $uri) = @$item; | ||
| 36 | if ($uri) { | ||
| 37 | push(@nav_html, "<a href=\"$uri\">$name</a>"); | ||
| 38 | } else { | ||
| 39 | push(@nav_html, $name); | ||
| 40 | } | ||
| 41 | } | ||
| 42 | $self->_append(join(" :: ", @nav_html) . "<hr/>\n"); | ||
| 43 | } | ||
| 44 | |||
| 45 | my $translator = new UR::Doc::Pod2Html; | ||
| 46 | my $title; | ||
| 47 | $translator->output_string($title); | ||
| 48 | $translator->parse_string_document("=pod\n\n".$self->title."\n\n=cut\n\n"); | ||
| 49 | $self->_append("<h1><a name=\"___top\"></a>$title</h1>\n"); | ||
| 50 | |||
| 51 | } | ||
| 52 | |||
| 53 | sub _render_index { | ||
| 54 | my $self = shift; | ||
| 55 | my @titles = grep { $_ and /./ } map { $_->title } $self->sections; | ||
| 56 | my $i = 0; | ||
| 57 | if (@titles) { | ||
| 58 | $self->_append("\n<ul>\n". | ||
| 59 | join("\n", map {"<li><a href=\"#___sec".($i++)."\">$_</a></li>"} @titles)."</ul>\n\n"); | ||
| 60 | } | ||
| 61 | } | ||
| 62 | |||
| 63 | sub _render_section { | ||
| 64 | my ($self, $section, $idx) = @_; | ||
| 65 | if (my $title = $section->title) { | ||
| 66 | $self->_append("<h1><a name=\"___sec$idx\" href=\"#___top\">$title</a></h1>\n"); | ||
| 67 | } | ||
| 68 | my $content = $section->content; | ||
| 69 | if ($section->format eq 'html') { | ||
| 70 | $self->_append($content); | ||
| 71 | } elsif ($section->format eq 'txt' or $section->format eq 'pod') { | ||
| 72 | $content = "\n\n=pod\n\n$content\n\n=cut\n\n"; | ||
| 73 | my $new_content; | ||
| 74 | my $translator = new UR::Doc::Pod2Html; | ||
| 75 | $translator->output_string($new_content); | ||
| 76 | $translator->parse_string_document($content); | ||
| 77 | $self->_append($new_content); | ||
| 78 | } else { | ||
| 79 | croak "Unknown section type " . $section->type; | ||
| 80 | } | ||
| 81 | $self->_append("<br/>\n"); | ||
| 82 | } | ||
| 83 | |||
| 84 | sub _render_footer { | ||
| 85 | my $self = shift; | ||
| 86 | $self->_append("</body></html>"); | ||
| 87 | } | ||
| 88 | |||
| 89 | sub generate_index { | ||
| 90 | my ($self, @command_trees) = @_; | ||
| 91 | return '' unless @command_trees; | ||
| 92 | |||
| 93 | my $html = "<h1>Command Index</h1><hr/>\n"; | ||
| 94 | $html .= $self->_generate_index_body(@command_trees); | ||
| 95 | return $html; | ||
| 96 | } | ||
| 97 | |||
| 98 | sub _generate_index_body { | ||
| 99 | my ($self, @command_trees) = @_; | ||
| 100 | return '' unless @command_trees; | ||
| 101 | |||
| 102 | my $html = "<ul>\n"; | ||
| 103 | for my $tree (@command_trees) { | ||
| 104 | my $name = $tree->{command_name_brief}; | ||
| 105 | my $uri = $tree->{uri}; | ||
| 106 | $html .= "<li><a href=\"$uri\">$name</a>\n"; | ||
| 107 | $html .= $self->_generate_index_body(@{$tree->{sub_commands}}); | ||
| 108 | $html .= "</li>\n"; | ||
| 109 | } | ||
| 110 | $html .= "</ul>\n"; | ||
| 111 | return $html; | ||
| 112 | } | ||
| 113 | |||
| 114 | 1; |