Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
694f7010
Kaydet (Commit)
694f7010
authored
Eyl 10, 1996
tarafından
Guido van Rossum
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Rewritten by Ka-Ping Yee.
üst
ba885ffa
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
160 additions
and
186 deletions
+160
-186
faq2html.py
Misc/faq2html.py
+160
-186
No files found.
Misc/faq2html.py
Dosyayı görüntüle @
694f7010
#! /usr/local/bin/python
#!/usr/local/bin/python
# Convert the Python FAQ to HTML
# A somewhat-generalized FAQ-to-HTML converter (by Ka-Ping Yee, 10 Sept 96)
import
string
# Reads a text file given on standard input or named as first argument, and
import
regex
# generates HTML 2.0 on standard output. Recognizes these constructions:
import
regsub
#
import
sys
# HTML element pattern at the beginning of a line
import
os
#
# section heading (<number><period>)+<space>
FAQ
=
'FAQ'
# numbered list element <1-2 spaces>(<number><period>)+<space>
# unnumbered list element <0-2 spaces><hyphen or asterisk><space>
chapterprog
=
regex
.
compile
(
'^
\
([1-9][0-9]*
\
)
\
. '
)
# preformatted section <more than two spaces>
questionprog
=
regex
.
compile
(
'^
\
([1-9][0-9]*
\
)
\
.
\
([1-9][0-9]*
\
)
\
. '
)
#
newquestionprog
=
regex
.
compile
(
'^Q
\
. '
)
# Heading level is determined by the number of (<number><period>) segments.
blankprog
=
regex
.
compile
(
'^[
\t
]*$'
)
# Blank lines force a separation of elements; if none of the above four
indentedorblankprog
=
regex
.
compile
(
'^
\
([
\t
]+
\
|[
\t
]*$
\
)'
)
# types is indicated, a new paragraph begins. A line beginning with many
underlineprog
=
regex
.
compile
(
'^==*$'
)
# spaces is interpreted as a continuation (instead of preformatted) after
eightblanksprog
=
regex
.
compile
(
'^
\
(
\
| *
\t
\
)'
)
# a list element. Headings are anchored; paragraphs starting with "Q." are
mailheaderprog
=
regex
.
compile
(
'^
\
(Subject
\
|Newsgroups
\
|Followup-To
\
|From
\
|Reply-To
\
|Approved
\
|Archive-name
\
|Version
\
|Last-modified
\
): +'
)
# emphasized, and those marked with "A." get their first sentence emphasized.
urlprog
=
regex
.
compile
(
'<URL:
\
([^>]*
\
)>'
)
#
ampprog
=
regex
.
compile
(
'&'
)
# Hyperlinks are created from references to:
aprog
=
regex
.
compile
(
'^A
\
. +'
)
# URLs, explicitly marked using <URL:scheme://host...>
qprog
=
regex
.
compile
(
'>Q
\
. +'
)
# other questions, of the form "question <number>(<period><number>)*"
qrefprog
=
regex
.
compile
(
'question +
\
([0-9]
\
.[0-9]+
\
)'
)
# sections, of the form "section <number>".
versionprog
=
regex
.
compile
(
'^Version: '
)
emailprog
=
regex
.
compile
(
'<
\
([^>@:]+@[^>@:]+
\
)>'
)
import
sys
,
string
,
regex
,
regsub
,
regex_syntax
regex
.
set_syntax
(
regex_syntax
.
RE_SYNTAX_AWK
)
def
main
():
print
'Reading lines...'
# --------------------------------------------------------- regular expressions
lines
=
open
(
FAQ
,
'r'
)
.
readlines
()
orditemprog
=
regex
.
compile
(
' ?([1-9][0-9]*
\
.)+ +'
)
print
'Renumbering in memory...'
itemprog
=
regex
.
compile
(
' ? ?[-*] +'
)
oldlines
=
lines
[:]
headingprog
=
regex
.
compile
(
'([1-9][0-9]*
\
.)+ +'
)
after_blank
=
1
prefmtprog
=
regex
.
compile
(
' '
)
chapter
=
0
blankprog
=
regex
.
compile
(
'^[
\t\r\n
]$'
)
question
=
0
questionprog
=
regex
.
compile
(
' *Q
\
. +'
)
chapters
=
[
'<OL>'
]
answerprog
=
regex
.
compile
(
' *A
\
. +'
)
questions
=
[
'<OL>'
]
sentprog
=
regex
.
compile
(
'(([^.:;?!(]|[.:;?!][^
\t\r\n
])+[.:;?!]?)'
)
for
i
in
range
(
len
(
lines
)):
line
=
lines
[
i
]
mailhdrprog
=
regex
.
compile
(
'^(Subject|Newsgroups|Followup-To|From|Reply-To'
if
after_blank
:
'|Approved|Archive-Name|Version|Last-Modified): +'
,
regex
.
casefold
)
n
=
chapterprog
.
match
(
line
)
urlprog
=
regex
.
compile
(
'<URL:([^&]+)>'
)
if
n
>=
0
:
addrprog
=
regex
.
compile
(
'<([^>@:]+@[^&@:]+)>'
)
chapter
=
chapter
+
1
qrefprog
=
regex
.
compile
(
'question +([1-9](
\
.[0-9]+)*)'
)
if
chapter
!=
1
:
srefprog
=
regex
.
compile
(
'section +([1-9][0-9]*)'
)
questions
.
append
(
'</UL>
\n
'
)
entityprog
=
regex
.
compile
(
'[&<>]'
)
question
=
0
lines
[
i
]
=
'<H2>'
+
line
[
n
:
-
1
]
+
'</H2>
\n
'
# ------------------------------------------------------------ global variables
chapters
.
append
(
'<LI> '
+
line
[
n
:])
body
=
[]
questions
.
append
(
'<LI> '
+
line
[
n
:])
ollev
=
ullev
=
0
questions
.
append
(
'<UL>
\n
'
)
element
=
content
=
secnum
=
version
=
''
afterblank
=
0
# ----------------------------------------------------- for making nested lists
def
dnol
():
global
body
,
ollev
ollev
=
ollev
+
1
if
body
[
-
1
]
==
'</li>'
:
del
body
[
-
1
]
body
.
append
(
'<ol>'
)
def
upol
():
global
body
,
ollev
ollev
=
ollev
-
1
body
.
append
(
ollev
and
'</ol></li>'
or
'</ol>'
)
# --------------------------------- output one element and convert its contents
def
spew
(
clearol
=
0
,
clearul
=
0
):
global
content
,
body
,
ollev
,
ullev
if
content
:
if
entityprog
.
search
(
content
)
>
-
1
:
content
=
regsub
.
gsub
(
'&'
,
'&'
,
content
)
content
=
regsub
.
gsub
(
'<'
,
'<'
,
content
)
content
=
regsub
.
gsub
(
'>'
,
'>'
,
content
)
n
=
questionprog
.
match
(
content
)
if
n
>
0
:
content
=
'<em>'
+
content
[
n
:]
+
'</em>'
if
ollev
:
# question reference in index
fragid
=
regsub
.
gsub
(
'^ +|
\
.? +$'
,
''
,
secnum
)
content
=
'<a href="#
%
s">
%
s</a>'
%
(
fragid
,
content
)
if
element
[
0
]
==
'h'
:
# heading in the main text
fragid
=
regsub
.
gsub
(
'^ +|
\
.? +$'
,
''
,
secnum
)
content
=
secnum
+
'<a name="
%
s">
%
s</a>'
%
(
fragid
,
content
)
n
=
answerprog
.
match
(
content
)
if
n
>
0
:
# answer paragraph
content
=
regsub
.
sub
(
sentprog
,
'<strong>
\\
1</strong>'
,
content
[
n
:])
body
.
append
(
'<'
+
element
+
'>'
+
content
)
body
.
append
(
'</'
+
element
+
'>'
)
content
=
''
while
clearol
and
ollev
:
upol
()
if
clearul
and
ullev
:
body
.
append
(
'</ul>'
);
ullev
=
0
# ---------------------------------------------------------------- main program
faq
=
len
(
sys
.
argv
)
>
1
and
sys
.
argv
[
1
]
and
open
(
sys
.
argv
[
1
])
or
sys
.
stdin
lines
=
faq
.
readlines
()
for
line
in
lines
:
if
line
[
2
:
9
]
==
'======='
:
# <hr> will appear *before*
body
.
append
(
'<hr>'
)
# the underlined heading
continue
continue
n
=
underlineprog
.
match
(
line
)
if
n
>=
0
:
n
=
orditemprog
.
match
(
line
)
lines
[
i
]
=
''
if
n
>
0
:
# make ordered list item
spew
(
0
,
'clear ul'
)
secnum
=
line
[:
n
]
level
=
string
.
count
(
secnum
,
'.'
)
while
level
>
ollev
:
dnol
()
while
level
<
ollev
:
upol
()
element
,
content
=
'li'
,
line
[
n
:]
continue
continue
n
=
questionprog
.
match
(
line
)
if
n
<
0
:
n
=
newquestionprog
.
match
(
line
)
-
3
n
=
itemprog
.
match
(
line
)
if
n
>=
0
:
if
n
>
0
:
# make unordered list item
question
=
question
+
1
spew
(
'clear ol'
,
0
)
number
=
'
%
d.
%
d'
%
(
chapter
,
question
)
if
ullev
==
0
:
body
.
append
(
'<ul>'
);
ullev
=
1
lines
[
i
]
=
'<A NAME="'
+
number
+
'"><H3>'
+
line
[
n
:]
element
,
content
=
'li'
,
line
[
n
:]
questions
.
append
(
'<LI><A HREF="#'
+
\
number
+
'">'
+
line
[
n
:])
# Add up to 4 continuations of the question
n
=
len
(
number
)
for
j
in
range
(
i
+
1
,
i
+
5
):
if
blankprog
.
match
(
lines
[
j
])
>=
0
:
lines
[
j
-
1
]
=
lines
[
j
-
1
]
+
'</H3></A>'
questions
[
-
1
]
=
\
questions
[
-
1
][:
-
1
]
+
'</A>
\n
'
break
questions
.
append
(
' '
*
(
n
+
2
)
+
lines
[
j
])
afterblank
=
0
continue
continue
afterblank
=
(
blankprog
.
match
(
line
)
>=
0
)
print
'Inserting list of chapters...'
n
=
headingprog
.
match
(
line
)
chapters
.
append
(
'</OL>
\n
'
)
if
n
>
0
:
# make heading element
for
i
in
range
(
len
(
lines
)):
spew
(
'clear ol'
,
'clear ul'
)
line
=
lines
[
i
]
secnum
=
line
[:
n
]
if
regex
.
match
(
sys
.
stderr
.
write
(
line
)
'^This FAQ is divided in the following chapters'
,
element
,
content
=
'h
%
d'
%
string
.
count
(
secnum
,
'.'
),
line
[
n
:]
line
)
>=
0
:
i
=
i
+
1
while
1
:
line
=
lines
[
i
]
if
indentedorblankprog
.
match
(
line
)
<
0
:
break
del
lines
[
i
]
lines
[
i
:
i
]
=
chapters
break
else
:
print
'*** Can
\'
t find header for list of chapters'
print
'*** Chapters found:'
for
line
in
chapters
:
print
line
,
print
'Inserting list of questions...'
questions
.
append
(
'</UL></OL>
\n
'
)
for
i
in
range
(
len
(
lines
)):
line
=
lines
[
i
]
if
regex
.
match
(
'^Here.s an overview of the questions'
,
line
)
>=
0
:
i
=
i
+
1
while
1
:
line
=
lines
[
i
]
if
indentedorblankprog
.
match
(
line
)
<
0
:
break
del
lines
[
i
]
lines
[
i
:
i
]
=
questions
break
else
:
print
'*** Can
\'
t find header for list of questions'
print
'*** Questions found:'
for
line
in
questions
:
print
line
,
# final cleanup
print
"Final cleanup..."
doingpre
=
0
for
i
in
range
(
len
(
lines
)):
# set lines indented by >= 8 spaces using PRE
# blank lines either terminate PRE or separate paragraphs
n
=
eightblanksprog
.
match
(
lines
[
i
])
if
n
<
0
:
n
=
mailheaderprog
.
match
(
lines
[
i
])
if
n
>=
0
:
if
versionprog
.
match
(
lines
[
i
])
>
0
:
version
=
string
.
split
(
lines
[
i
])[
1
]
if
doingpre
==
0
:
lines
[
i
]
=
'<PRE>
\n
'
+
lines
[
i
]
doingpre
=
1
continue
continue
n
=
blankprog
.
match
(
lines
[
i
])
if
n
>=
0
:
n
=
0
# print '*** ', lines[i-1], doingpre
if
not
secnum
:
# haven't hit body yet
if
doingpre
==
1
:
n
=
mailhdrprog
.
match
(
line
)
lines
[
i
]
=
'</PRE><P>
\n
'
v
=
version
and
-
1
or
regex
.
match
(
'Version: '
,
line
)
doingpre
=
0
if
v
>
0
and
not
version
:
version
=
line
[
v
:]
if
n
<=
0
and
element
!=
'li'
:
# not pre if after a list item
n
=
prefmtprog
.
match
(
line
)
if
n
>
0
:
# make preformatted element
if
element
==
'pre'
:
content
=
content
+
line
else
:
else
:
lines
[
i
]
=
'<P>
\n
'
spew
(
'clear ol'
,
'clear ul'
)
element
,
content
=
'pre'
,
line
continue
continue
# & -> &
if
blankprog
.
match
(
line
)
>
0
:
# force a new element
n
=
ampprog
.
search
(
lines
[
i
])
spew
()
if
n
>=
0
:
element
=
''
lines
[
i
]
=
regsub
.
gsub
(
ampprog
,
'&'
,
lines
[
i
])
elif
element
:
# continue current element
# no continue - there might be other changes to the line...
content
=
content
+
line
else
:
# no element; make paragraph
# zap all the 'Q.' and 'A.' leaders - what happened to the
spew
(
'clear ol'
,
'clear ul'
)
# last couple?
element
,
content
=
'p'
,
line
n
=
qprog
.
search
(
lines
[
i
])
if
n
>=
0
:
spew
()
# output last element
lines
[
i
]
=
regsub
.
sub
(
qprog
,
'>'
,
lines
[
i
])
# no continue - there might be other changes to the line...
body
=
string
.
joinfields
(
body
,
''
)
body
=
regsub
.
gsub
(
urlprog
,
'<a href="
\\
1">
\\
1</a>'
,
body
)
n
=
aprog
.
search
(
lines
[
i
])
body
=
regsub
.
gsub
(
addrprog
,
'<a href="mailto:
\\
1">
\\
1</a>'
,
body
)
if
n
>=
0
:
body
=
regsub
.
gsub
(
qrefprog
,
'<a href="#
\\
1">question
\\
1</a>'
,
body
)
lines
[
i
]
=
regsub
.
sub
(
aprog
,
''
,
lines
[
i
])
body
=
regsub
.
gsub
(
srefprog
,
'<a href="#
\\
1">section
\\
1</a>'
,
body
)
# no continue - there might be other changes to the line...
print
'<!doctype html public "-//IETF//DTD HTML 2.0//EN"><html>'
# patch up hard refs to questions
print
'<head><title>Python Frequently-Asked Questions v'
+
version
n
=
qrefprog
.
search
(
lines
[
i
])
print
"</title></head><body>(This file was generated using Ping's"
if
n
>=
0
:
print
'<a href="faq2html.py">faq2html.py</a>.)'
lines
[
i
]
=
regsub
.
sub
(
qrefprog
,
print
body
+
'</body></html>'
'<A HREF="#
\\
1">question
\\
1</A>'
,
lines
[
i
])
# no continue - there might be other changes to the line...
# make <URL:...> into actual links
n
=
urlprog
.
search
(
lines
[
i
])
if
n
>=
0
:
lines
[
i
]
=
regsub
.
gsub
(
urlprog
,
'<A HREF="
\\
1">
\\
1</A>'
,
lines
[
i
])
# no continue - there might be other changes to the line...
# make <user@host.domain> into <mailto:...> links
n
=
emailprog
.
search
(
lines
[
i
])
if
n
>=
0
:
lines
[
i
]
=
regsub
.
gsub
(
emailprog
,
'<A HREF="mailto:
\\
1">
\\
1</A>'
,
lines
[
i
])
# no continue - there might be other changes to the line...
lines
[
0
:
0
]
=
[
'<HTML><HEAD><TITLE>Python Frequently Asked Questions v'
,
version
,
'</TITLE>
\n
'
,
'</HEAD><body>
\n
'
,
'(This file was generated using
\n
'
,
'<A HREF="faq2html.py">faq2html.py</A>.)<P>
\n
'
]
lines
.
append
(
'<P></BODY></HTML>
\n
'
)
print
'Writing html file...'
f
=
open
(
FAQ
+
'.html'
,
'w'
)
for
line
in
lines
:
f
.
write
(
line
)
f
.
close
()
print
'Done.'
main
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment