App-Codit

 view release on metacpan or  search on metacpan

lib/App/Codit/Plugins/Exporter.pm  view on Meta::CPAN

	croak "Outfile not specified" if $file eq '';
	my %params = ();
	for (sort keys %options) {
		my $val = $options{$_};
		$params{$_} = $val if $val;
	}
	
	#exporting
	my $widg = $mdi->docWidget;
	my $hl = Syntax::Kamelon->new(
		syntax => $widg->cget('-syntax'),
		formatter => ['HTML4', %params],
	);
	$hl->Reset;
	my $text = $widg->get('1.0', 'end - 1c');
	$hl->Parse($text);
	if (open my $fh, '>', $file) {
		print $fh $hl->Format;
		close $fh
	} else {
		$self->logWarning("Can not open '$file'");
	}
}

sub export2png {
	my $self = shift;
	my $mdi = $self->mdi;

	#setting initial values
	my %iv = ();
	my $name = $mdi->docSelected;
	return unless defined $name;
	my $widg = $mdi->docWidget;

	my $folder = $self->configGet('-configfolder');
	if (-e "$folder/export2pngrc") {
		my $cff = $self->extGet('ConfigFolder');
		%iv = $cff->loadHash('export2pngrc', 'exporterpng');
	} else {
		$iv{'background'} = $widg->Subwidget('XText')->cget('-background');
		$iv{'foreground'} = $widg->Subwidget('XText')->cget('-foreground');
		$iv{'themefile'} = $self->extGet('ConfigFolder')->ConfigFolder . '/highlight_theme.ctt';
		$iv{'font'} = $self->configGet('-contentfont');
		$iv{'linenumbers'} = 1;
		$iv{'margins'} = 10;
		$iv{'maxwidth'} = 0;
		$iv{'tabsize'} = 3;
	}
	$iv{'outfile'} = "$name.png";

	#popping the form
	my %options = $self->popForm(
#		-acceptempty => 1,
		-initialvalues => \%iv,
		-oktext => 'Export',
		-structure => [
			'*section' => 'Output',
			outfile => ['file', 'File', -width => 50],
			'*end',
			'*section' => 'Theme',
			'*frame',
			background => ['color', 'Background', -width => 8],
			'*column',
			foreground => ['color', 'Foreground', -width => 8],
			'*end',
			themefile => ['file', 'Theme file'],
			font => ['font', 'Font'],
			'*end',
			'*section' => 'Features',
			linenumbers => ['boolean', 'Line numbers'],
			tabsize => ['spin', 'Tab size', -width => 6],
			'*column',
			maxwidth => ['spin', 'Max width', -width => 6],
			margins => ['spin', 'Margins', -width => 6],
			'*end',
		],
		-title => 'Export to PNG',
	);
	return unless %options;

	#saving options
	my $cff = $self->extGet('ConfigFolder');
	$cff->saveHash('export2pngrc', 'exporterpng', %options);

	#checking data
	my $file = delete $options{'outfile'};
	croak "Outfile not specified" if $file eq '';
	my $tabstr = '';
	for (1 .. $options{'tabsize'}) { $tabstr = "$tabstr " }
	$self->background(Imager::Color->new($options{'background'}));
	$self->imargin($options{'margins'});
	$self->linenumbers($options{'linenumbers'});
	$self->linecolumn(length($widg->linenumber('end - 1c')));
	$self->maxwidth($options{'maxwidth'});
	$self->tabstring($tabstr);

	#converting data
	my $art = $self->extGet('Art');
	my $l = $self->Label(-font => $options{'font'});
	my $f = $l->cget('-font');
	my $fntnorm = $widg->fontCompose($f, -slant => 'roman', -weight => 'normal');
	my $fntbold = $widg->fontCompose($f, -slant => 'roman', -weight => 'bold');
	my $fntbolditalic = $widg->fontCompose($f, -slant => 'italic', -weight => 'bold');
	my $fntitalic = $widg->fontCompose($f, -slant => 'italic', -weight => 'normal');

	my $linespace = $self->fontMetrics($f, '-linespace');
	my $desc = $self->fontMetrics($f, '-descent');

	$l->destroy;

	my $foreground =  Imager::Color->new($options{'foreground'});

	$self->xpos($self->imargin);
	$self->ypos($self->imargin + $linespace - $desc);
	$self->iwidth($self->imargin * 2);
	$self->iheight(($self->imargin * 2) + $linespace);

	$self->image($self->inew($self->iwidth, $self->iheight));

	#setting up tags
	my $thmfl = $options{'themefile'};



( run in 0.781 second using v1.01-cache-2.11-cpan-e1769b4cff6 )