File: py2wiki.py

package info (click to toggle)
yade 2019.01a-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 16,568 kB
  • sloc: cpp: 56,330; python: 30,148; ansic: 6,463; sh: 123; makefile: 56
file content (78 lines) | stat: -rw-r--r-- 2,525 bytes parent folder | download | duplicates (10)
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
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python
# encoding: utf-8
#
# 2008 © Václav Šmilauer <eudoxos@arcig.cz>

"""
Import commented python source, format it for yade.wikia.com by interspersing <source> and literal text in comments.
If given a second files (which is a c++ file), the comments will be taken from the python source,
but the code from the c++ file.

There are special marks for comments in python: "##" for empty line
or "## some text" for non-empty line (the first space is stripped)

Special marks for c++ comments are //@, but their contents is flushed;
they serve merely to synchronize with python comments.

Adjacent comment lines and adjacent code lines will be merged.

Leading tabs in the code are replaced by unicode non-breakable spaces
(<source> would otherwise eat leading spaces of the first line)

Usage 1: generate simple-scene.py formatted for wiki

   ./py2wiki simple-scene.py

Usasge 2: generate SimpleScene.cpp formatted for wiki with comments from simple-scene.py

   ./py2wiki simple-scene.py ../extra/SimpleScene.cpp 
"""

import sys,re
class Text:
	def __init__(self,txt):
		self.text=[txt]
class Code(Text): pass
class Comment(Text): pass
pg=[]
commentPatterns=(r'\s*##($|\s(.*)$)',r'\s*//@((.*))$')
codeLangs=('python','cpp')
assert(len(sys.argv)<=3)
for i in range(0,len(sys.argv)-1):
	pg.append([Comment('')])
	sys.stderr.write(str(i)+': '+sys.argv[i+1]+'\n')
	for l in open(sys.argv[i+1]):
		l=l[:-1]
		m=re.match(commentPatterns[i],l)
		if m: ll=Comment(m.group(1)[1:] if (len(m.group(1))>0 and m.group(1)[0]==' ') else m.group(1))
		else: ll=Code(l)
		if pg[i][-1].__class__==ll.__class__:
			pg[i][-1].text.append(ll.text[0])
		else: pg[i].append(ll)

# replace tabs by 8 _nonbreakable_ spaces (\xc2 in utf-8) in code
# strip leading/trailing blank lines
for p in pg:
	for l in p:
		if l.__class__==Code('').__class__:
			l.text=[ll.replace('\t',8*' ') for ll in l.text]
			while len(l.text)>0 and l.text[-1]=='': l.text=l.text[:-1]
			while len(l.text)>0 and l.text[0]=='': l.text=l.text[1:]

table=False
showCode=len(pg)-1
for i in range(len(pg[0])):
	ll=pg[0][i]
	if ll.__class__==Comment('').__class__:
		for l in ll.text: print l
	if ll.__class__==Code('').__class__:
		if table: print '<table><tr>'
		for j in (range(len(pg)) if table else [showCode]):
			print '%s<source lang="%s">'%('<td>' if table else '',codeLangs[j])
			#if pg[j][i].text[0][0]==' ': print ' ',
			for l in pg[j][i].text: print l
			print '</source>%s'%('</td>' if table else '')
		if table: print '</tr></table>'