www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.announce - D doc To Devhelp

reply bioinfornatics <bioinfornatics fedoraproject.rog> writes:
Dear,
In first i do not know where put this message i hope here is good.
I have wrote a little python script for add D doc into devhelp
why python, because it is a little script and like is not need to be
build for use it. it is most easier for add this little tools to your
build system. why not a new target devehelp into your makefile :-)


-------------------------------------------------
#!/usr/bin/env python

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import getopt
import os
import sys
from subprocess import Popen, PIPE, STDOUT
from BeautifulSoup import BeautifulSoup, Tag

project_name        =3D ""
project_source_dir  =3D ""
author              =3D ""
prefix              =3D "/usr/share" # could be ~/.local/share
install_dir         =3D ""
name                =3D ""
keywords_content    =3D ""
index_html          =3D ""
devhelp_file        =3D ""
keywords_content    =3D ""
d_compiler          =3D "ldc2"
d_files             =3D ()
keywords            =3D []

def basename( file_path , with_extention =3D False):
    result =3D ""
    if( with_extention ):
        result =3D "".join( file_path.split( os.sep )[-1] )
    else:
        result =3D ".".join( file_path.split( os.sep )[-1].split( "."
)[0:-1] )
    return result
   =20
def finder( directory =3D "/", extentions =3D [] ):
    file_list  =3D []
    for dirname, dirnames, filenames in os.walk( directory ):
        for filename in filenames:
            f =3D os.path.join(dirname, filename)
            if f.split('.')[-1] in extentions:
                file_list.append( f )
    return tuple( file_list )

def usage():
    print "-h --help                        Display this message"
    print "-a --name=3D<author name>          Set author name"
    print "-c --compiler=3D<compiler name>    Set compiler default: ldc2"
    print "-n --name=3D<project name>         Set project name"
    print "-s --source=3D<source dir>         Set source dir to scan"
    print "-p --prefix=3D<prefix dir>         Set prefix path for to write
devhelp book default: /usr/share"

def main(argv):
    global author
    global d_compiler
    global project_name
    global project_source_dir
    global prefix
    try:
        opts, args =3D getopt.getopt(argv, "ha:c:n:s:p", ["help",
"--author=3D", "--compiler=3D", "name=3D", "source=3D", "prefix=3D"])
    except getopt.GetoptError:
        usage()
        sys.exit(2)      =20
       =20
    for opt, arg in opts:
        if opt in ("-h", "--help"):
            usage()
            sys.exit()
        elif opt in ("-a", "--author=3D"):
            author =3D arg
        elif opt in ("-c", "--compiler=3D"):
            d_compiler =3D arg
        elif opt in ("-n", "--name=3D"):
            project_name =3D arg
        elif opt in ("-s", "--source=3D"):
            project_source_dir =3D arg
        elif opt in ("-p", "--prefix=3D"):
            prefix =3D arg
 =20
def keyword_maker( doc_html_file ):
    global keywords
    html_content =3D []
    with open( doc_html_file, "r" ) as in_html:
        html_content =3D in_html.read()
    mySoup          =3D BeautifulSoup( html_content )
    dts             =3D mySoup.findAll( "dt" )
    for dt in dts:
        type_name =3D ""
        decl_name =3D ""
        if( dt.big is not None ):
            if( isinstance( dt.big.contents[0], Tag) ):
                type_name =3D dt.big.contents[0].text
            else:
                type_name =3D dt.big.contents[0]
            if( dt.big.u is not None ):
                decl_name =3D  dt.big.u.text
                link_html =3D basename( doc_html_file, with_extention =3D
True )
                keywords.append( [type_name, decl_name, link_html ] ) #
global burk



if __name__ =3D=3D "__main__":
    # Parse command line
    main( sys.argv[1:] )
    # Use initialized var for set file and dir
    install_dir =3D os.path.join( prefix, "devhelp/books/", project_name )
    index_html  =3D os.path.join( install_dir, "index.html" )
    devhelp_file=3D os.path.join( install_dir, project_name + ".devhelp2"
)
    d_files     =3D finder( "/usr/include/d/std", ["d", "di"] )
    # create dir if do no exist
    if( not os.path.exists( install_dir ) ):
        os.mkdir( install_dir )
    with open( index_html, "w" ) as out_html,  open( devhelp_file, "w" )
as out_xml:
        # Add html header
        out_html.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01
Transitional//EN\">\n")
        out_html.write("\t<html>\n")
        out_html.write("\t<meta http-equiv=3D\"Content-Type\" content=3D
\"text/html; charset=3DUTF-8\">\n\n")
        out_html.write("\t\t<title>"+ project_name +"</title>\n")
        out_html.write("\t</meta>\n")
        out_html.write("\t<body>\n")
        # Add xml header
        out_xml.write("<?xml version=3D\"1.0\" encoding=3D\"utf-8\"
standalone=3D\"no\"?>\n")
        out_xml.write("<!DOCTYPE book PUBLIC \"-//W3C//DTD HTML 4.01
Transitional//EN\" \"\">\n")
        out_xml.write("<book xmlns=3D\"http://www.devhelp.net/book\"
title=3D\"%s\" link=3D\"index.html\" author=3D\"%s\" name=3D\"%s\" version=
=3D\"2\"
language=3D\"d\">\n" % (project_name, author, project_name) )
        out_xml.write("\t<chapters>\n")
        # Create doc file from source and in same time complete
index.html <project>.devhelp2 file
        for f in d_files:
            try:
                dc =3D Popen( [ d_compiler, "-o-", f, "-Dd=3D" + install_di=
r
], stdout=3DPIPE, stderr=3DSTDOUT )
                dc.communicate()               =20
            except Exception as e:
                print e
            base_name =3D basename( f ) # take file name without his path
and without extention
            html_file =3D os.path.join( install_dir, base_name +".html" )
            if( os.path.exists( html_file ) ):
                out_html.write("\t\t<a href=3D\"" + base_name +".html\">"
+ base_name + "</a><br>\n")
                out_xml.write("\t\t<sub name=3D\""+ base_name +"\" link=3D
\""+ base_name + ".html\"/>\n")
                keyword_maker( html_file )
        # Add html footer
        out_html.write("\t</body>\n")
        out_html.write("</html>\n")
        # Add xml footer
        out_xml.write("\t</chapters>\n")
        out_xml.write("\t<functions>\n")
        for item in keywords:
            out_xml.write("\t\t<keyword type=3D\"%s\" name=3D\"%s\" link=3D=
\"%
s\"/>\n" % ( item[0], item[1], item[2] ) )
        out_xml.write("\t</functions>\n")
        out_xml.write("</book>\n")
   =20
Dec 08 2011
parent bioinfornatics <bioinfornatics fedoraproject.rog> writes:
Le vendredi 09 d=C3=A9cembre 2011 =C3=A0 05:31 +0100, bioinfornatics a =C3=
=A9crit :
 Dear,
 In first i do not know where put this message i hope here is good.
 I have wrote a little python script for add D doc into devhelp
 why python, because it is a little script and like is not need to be
 build for use it. it is most easier for add this little tools to your
 build system. why not a new target devehelp into your makefile :-)
=20
=20
 -------------------------------------------------

I have put this tool into github: https://github.com/bioinfornatics/DdocToDevhelp Example of use: DdocToDevhelp -n Phobos -s /usr/include/d/std/
Dec 09 2011