File: ys2book.pl

package info (click to toggle)
yacas 1.3.6-2
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 7,176 kB
  • ctags: 3,520
  • sloc: cpp: 13,960; java: 12,602; sh: 11,401; makefile: 552; perl: 517; ansic: 381
file content (66 lines) | stat: -rw-r--r-- 1,383 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/perl

# Usage: perl ys2book.pl [-strip] < file.ys > file.chapt.txt

# This will make a documentation chapter out of a Yacas script file.
# All comments /** ... */  or /// ... become doc text.
# Comments such as // ... remain code comments. All Yacas code becomes code sample code blocks unless -strip option is given.
# Multiple indentation of code blocks: TAB is converted to 2 spaces.

$inside_comment = 0;

$want_code = ("@ARGV" =~ /-strip/) ? 0 : 1;

sub print_code_line
{
# FIXME: need to wrap long lines of code
	my $text = shift;
	if ($want_code)
	{
		print $text;
	}
}

while(<STDIN>)
{
	chomp;
	if ($inside_comment)
	{
		if (m|(.*)\*/\s*(.*)$|)	# */ comment finished, possibly some code follows
		{
			$inside_comment = 0;
			print "$1\n";
			print_code_line("$2\n");
		}
		else	# still inside a /* */ block
		{
			s/^\s*\*//;	# remove Ayal's initial *'s
			print "$_\n";
		}
	}
	else
	{
		if (m|^\s*///+\s*([^\s/].*)$|)	# /// comment
		{
			print "$1\n";
		}
		elsif (m|^\s*///+\s*$|)	# empty /// comment
		{
			print "\n";
		}
		elsif (m|^\s*/\*\*\s*([^\s].*)\*/$|)	# /** comment started and finished on the same line
		{
			print "$1\n";
		}
		elsif (m|^\s*/\*\*\s*([^\s].*)$|)	# /** comment started
		{
			print "$1\n";
			$inside_comment = 1;
		}
		else
		{	 # Code block: convert indentation: TAB <-- 2 spaces
			s/\t/  /g;
			print_code_line($_);
		}
	}
}