File indexing completed on 2024-07-14 15:05:10

0001 # -*- coding: utf-8 -*-
0002 #     Copyright 2007-8 Jim Bublitz <jbublitz@nwinternet.com>
0003 #     Copyright 2008   Simon Edwards <simon@simonzone.com>
0004 #
0005 # This program is free software; you can redistribute it and/or modify
0006 # it under the terms of the GNU General Public License as published by
0007 # the Free Software Foundation; either version 2 of the License, or
0008 # (at your option) any later version.
0009 #
0010 # This program is distributed in the hope that it will be useful,
0011 # but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013 # GNU General Public License for more details.
0014 #
0015 # You should have received a copy of the GNU General Public License
0016 # along with this program; if not, write to the
0017 # Free Software Foundation, Inc.,
0018 # 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
0019 
0020 from string import ascii_lowercase
0021 import re
0022 
0023 deleteWords = ["@attention", "@exception", "@name", "\\name", "@e", "@port4", "@private", "@reimp",\
0024     "@{", "@}", "@class", "@copydoc", "@bc", "@file", "\\file", "@overload", "\\overload",\
0025     "\\e", "@brief", "\\brief", "\\brief", "\\short", "@short", "@c", "\\c", "@ref",\
0026     "\\ref",  "@enum", "\\par", "\\link", "\\endlink", "\\}", "\\n"]
0027 
0028 deleteLine  = ["@defgroup", "@namespace", "\\relates", "\\subsection", "@ingroup", \
0029     "\\ingroup","\\namespace","\\headerfile"]
0030 
0031 replaceWord = {
0032     "@todo": "To do:",
0033     "\\todo": "To do:",
0034     "@flags": "<b>flags</b> - ",
0035     "@version": "<b>Version:</b>",
0036     "@url": "<b>url</b>",
0037     "(\b": "(",
0038     "\\verbatim": "<pre>",
0039     "\\endverbatim": "</pre>",
0040     r'%KDE': "KDE",
0041     r'%Solid': "Solid"
0042 }
0043 lgpllink = '<a href="http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html#SEC1">LGPLv2</a>'
0044 gpllink = '<a href="http://www.gnu.org/licenses/old-licenses/gpl-2.0.html#SEC1">GPLv2</a>'
0045 rawReplaceWord = {
0046     "@lgpl": lgpllink,
0047     "\\lgpl": lgpllink,
0048     "@gpl": gpllink,
0049     "@lgpl<br>": lgpllink,
0050     "@gpl<br>": gpllink,
0051     "<code>": "<pre>",
0052     "</code>": "</pre>"
0053 }                   
0054                 
0055 boldWordNL  =  ["@value", "@var", ]
0056 
0057 boldWord    =  ["@a", "\\a", "@p", "\\p", "@b", "\\b", "@em"]
0058 
0059 def escapeHtml(s):
0060     return s.replace('&','&amp;').replace('<','&lt;').replace('>','&gt;')
0061 
0062 def escapeDoyxgenHtml(s):
0063     es = escapeHtml(s.replace('\\<','<').replace('\\>','>'))
0064     es = es.replace('&lt;br&gt;','<br>')
0065     es = es.replace('&lt;p&gt;','<p>')
0066     es = es.replace('&lt;/p&gt;','</p>')
0067     es = es.replace('&lt;b&gt;','<b>')
0068     es = es.replace('&lt;/b&gt;','</b>')
0069     return es
0070     
0071 ahref_re = re.compile("&lt;a href.*?&gt;",re.IGNORECASE)
0072 def handleTags(x):
0073     i = 0
0074     m = ahref_re.search(x,i)
0075     parts = []
0076     while m:
0077         parts.append(x[i:m.start()])
0078         parts.append(m.group().replace('&lt;','<').replace('&gt;','>'))
0079         i = m.end()
0080         m = ahref_re.search(x,i)
0081     parts.append(x[i:])
0082     return (''.join(parts)).replace('&lt;/a&gt;','</a>')
0083     
0084 class Reduce(object):
0085     def __init__ (self):
0086         pass
0087 
0088     def do_txt (self, txt):
0089         txt = txt.replace ('/**<', '')
0090         txt = txt.replace ('/**', '')
0091         txt = txt.replace ('*/', '')
0092         txt = txt.replace ('\n*', '\n')
0093         txt = txt.replace ('///<', '')
0094         txt = txt.replace ('///', '')
0095         return self.fixDoc (txt.strip ())
0096         
0097     def replace (self, word):
0098         if word in replaceWord:
0099             return replaceWord [word]
0100         else:
0101             return word
0102     
0103     def delete (self, line):
0104         for word in deleteLine:
0105             if word in line:
0106                 return True
0107         return False            
0108     
0109     
0110     def fixDoc (self, txt):
0111         STATE_NORMAL = 0
0112         STATE_PARAM = 1
0113         STATE_PARAM_TRAILING = 2
0114         STATE_DL = 3
0115         STATE_CODE = 4
0116         STATE_LICENSES = 5
0117         STATE_NBULLET = 6
0118         STATE_NBULLET_TRAILING = 7
0119         STATE_SKIP = 8
0120         
0121         state = STATE_NORMAL
0122 
0123         txt = txt.replace ("::", ".")
0124         lines = txt.split ("\n")
0125         doclines = [line for line in lines if not self.delete (line)]
0126 
0127         for i in range (len (doclines)):
0128             if state==STATE_SKIP:
0129                 break
0130             strippedline = doclines [i].strip ()
0131             
0132             parts = strippedline.split(" ")
0133             firstword = parts[0] if len(parts)!=0 else None
0134             
0135             newdocline = []
0136             if state==STATE_PARAM:
0137                 if strippedline=="":
0138                     newdocline.append("</td></tr>")
0139                     state = STATE_PARAM_TRAILING
0140                 elif firstword not in boldWord and firstword!="@param" and \
0141                         (firstword.startswith('@') or firstword.startswith('\\')):
0142                     newdocline.append("</td></tr>")
0143                     newdocline.append("</table></dl>\n<p>")
0144                     state = STATE_NORMAL
0145                     
0146             elif state==STATE_PARAM_TRAILING:
0147                 if strippedline!="" and not firstword=="@param":
0148                     newdocline.append("</table></dl>\n<p>")
0149                     state = STATE_NORMAL
0150                     
0151             elif state==STATE_NBULLET:
0152                 if strippedline=="":
0153                     newdocline.append("</li>")
0154                     state = STATE_NBULLET_TRAILING
0155                 elif firstword not in boldWord and firstword!="-#" and \
0156                         (firstword.startswith('@') or firstword.startswith('\\')):
0157                     newdocline.append("</li></ol>")
0158                     newdocline.append("\n<p>")
0159                     state = STATE_NORMAL
0160                     
0161             elif state==STATE_NBULLET_TRAILING:
0162                 if strippedline!="" and not firstword=="-#":
0163                     newdocline.append("</ol>\n<p>")
0164                     state = STATE_NORMAL
0165                                 
0166             if state==STATE_DL:
0167                 if strippedline=="" or firstword.startswith('@') or firstword.startswith('\\'):
0168                     newdocline.append("</dd></dl>")
0169                     state = STATE_NORMAL
0170             
0171             if state==STATE_LICENSES:
0172                 if strippedline=="" or (firstword.startswith('@') and firstword!="@lgpl") or \
0173                         (firstword.startswith('\\') and firstword!="\\lgpl"):
0174                     newdocline.append("</dd></dl>")
0175                     state = STATE_NORMAL
0176             
0177             if state==STATE_CODE:
0178                 if firstword=="\\endcode" or firstword=="@endcode":
0179                     newdocline.append("</pre>")
0180                     state = STATE_NORMAL
0181                 else:
0182                     newdocline.append(escapeHtml(doclines[i]))
0183     
0184             elif i and not strippedline:
0185                 if state==STATE_NORMAL:
0186                     newdocline.append("</p>\n<p>")
0187         
0188             else:
0189                 rawline = strippedline.split(" ")
0190     
0191                 # take care of "@ markup" cases and remove blanks
0192                 line = []
0193                 atFlag = False
0194                 for word in rawline:
0195                     if word == "@":
0196                         atFlag = True
0197                     elif word != " ":
0198                         if atFlag:
0199                             line.append ("@%s" % word)
0200                             atFlag = False
0201                         elif word not in deleteWords:
0202                             line.append (word)
0203     
0204                 line = [self.replace (word) for word in line]
0205                 j = 0
0206                 originallinelen = len(line)
0207                 while j < originallinelen:
0208                     word = line [j]
0209                     
0210                     if word=='@author' or word=='\\author':
0211                         line [j] = '\n<dl class="author" compact><dt><b>Author:</b></dt><dd>'
0212                         line.append("</dd></dl>")
0213                         
0214                     elif word=='@authors' or word=='\\authors':
0215                         line [j] = '\n<dl compact><dt><b>Author(s):</b></dt><dd>'
0216                         state = STATE_DL
0217                         
0218                     elif word=='@maintainers' or word=='\\maintainers':
0219                         line [j] = '\n<dl compact><dt><b>Maintainer(s):</b></dt><dd>'
0220                         state = STATE_DL
0221                         
0222                     elif word=='@licenses' or word=='\\licenses':
0223                         line [j] = '\n<dl compact><dt><b>License(s):</b></dt><dd>'
0224                         state = STATE_LICENSES
0225                         
0226                     elif word=='@return' or word=='\\return' or word=='@returns' or word=='\\returns':
0227                         line [j] = '<dl class="return" compact><dt><b>Returns:</b></dt><dd>'
0228                         state = STATE_DL
0229                         
0230                     elif word=='@see' or word=='\\see':
0231                         line [j] = '<dl class="see" compact><dt><b>See also:</b></dt><dd>'
0232                         state = STATE_DL
0233                         
0234                     elif word=='@since' or word=='\\since':
0235                         line [j] = '<dl class="since" compact><dt><b>Since:</b></dt><dd>'
0236                         state = STATE_DL
0237                     
0238                     elif word=='@note' or word=='\\note':
0239                         line [j] = '<dl class="note" compact><dt><b>Note:</b></dt><dd>'
0240                         state = STATE_DL
0241                     
0242                     elif word=='@internal' or word=='\\internal':
0243                         line [j] = '<dl class="internal" compact><dt><b>Internal:</b></dt><dd>'
0244                         state = STATE_DL
0245                         
0246                     elif word=='@deprecated' or word=='\\deprecated':
0247                         line [j] = '<dl class="deprecated" compact><dt><b>Deprecated:</b></dt><dd>'
0248                         state = STATE_DL
0249                     
0250                     elif word=='@obsolete' or word=='\\obsolete':
0251                         line [j] = '<dl class="obsolete" compact><dt><b>Obsolete:</b></dt><dd>'
0252                         state = STATE_DL
0253                         
0254                     elif word=='@warning' or word=='\\warning':
0255                         line [j] = '<dl class="warning" compact><dt><b>Warning:</b></dt><dd>'
0256                         state = STATE_DL
0257                         
0258                     elif word=='@code' or word=='\\code':
0259                         line[j] = '<pre class="fragment">'
0260                         state = STATE_CODE                    
0261 
0262                     elif word in rawReplaceWord:
0263                         line[j] = rawReplaceWord[word]
0264                         
0265                     elif word=="-#":
0266                         if state==STATE_NORMAL:
0267                             openList = '</p><ol type="1">'
0268                         else:
0269                             openList = ""
0270                         
0271                         line [j] = openList + '<li>'
0272                         state = STATE_NBULLET
0273                         
0274                     elif word=="\\page":    # Cut the dox off at this point.
0275                         line[j] = ''
0276                         line = line[0:j+1]
0277                         doclines = doclines[0:i+1]
0278                         state = STATE_SKIP
0279                         break
0280                     
0281                     elif j < len(line)-1:
0282                     
0283                         nextWord = line [j + 1]
0284                         if word in boldWord:
0285                             line [j] = ""
0286                             line [j + 1] = "<b>%s</b>" % escapeHtml(nextWord)
0287                             j += 1
0288                             
0289                         elif word=="@param" or word=="\\param":
0290                             line [j] = ""
0291                             if state==STATE_NORMAL:
0292                                 openTable = '</p><dl compact><dt><b>Parameters:</b></dt><dd>\n<table border="0" cellspacing="2" cellpadding="0">'
0293                             else:
0294                                 openTable = ""
0295                             
0296                             line [j + 1] = openTable + '\n<tr><td></td><td valign="top"><em>%s</em>&nbsp;</td><td>' % nextWord
0297                             j += 1
0298                             state = STATE_PARAM
0299                             
0300                         elif word in boldWordNL:
0301                             line [j] = ""
0302                             line [j + 1] = "\n<b>%s</b> -" % escapeHtml(nextWord)
0303                             j += 1
0304                             
0305                         elif word=="@mainpage" or word=="\\mainpage":
0306                             line [j] = ""
0307                             line [j + 1] = "<h2>" + line[j + 1]
0308                             j += 1
0309                             line.append ("</h2>")
0310                         
0311                         elif word in ["@li", "\li"]:
0312                             line [j] = "<li>"
0313                             line.append ("</li>")
0314                             
0315                         elif word in ["@section", "\section"]:
0316                             line [j] = ""
0317                             if nextWord [0] in ascii_lowercase and j < (len (line) - 2):
0318                                 line [j + 1] = ""
0319                                 line [j + 2] = "<b>%s" % escapeHtml(line[j + 2])
0320                                 j += 2
0321                             else:                        
0322                                 line [j + 1] = "<b>%s" % escapeHtml(nextWord)
0323                                 j += 1
0324                             line.append ("</b>")
0325                         elif word=='\\image' or word=='@image':
0326                             line[j] = ''
0327                             line[j+1] = ''
0328                             line[j+2] = '<div align="center"><img src="../images/' + line[j+2] + '" /><p><strong>'
0329                             line.append ('</strong></p></div>')
0330                             j += 2
0331 
0332                         else:                        
0333                             line[j] = escapeDoyxgenHtml(line[j])
0334                     else:
0335                         line[j] = escapeDoyxgenHtml(line[j])
0336                     j += 1
0337                 
0338                 line = [word for word in line if word]                           
0339                 newdocline.append(" ".join (line))
0340             doclines[i] = " ".join(newdocline)
0341     
0342         if state==STATE_PARAM:
0343             doclines.append("</td></tr>")
0344             state = STATE_PARAM_TRAILING
0345         if state==STATE_PARAM_TRAILING:
0346             doclines.append("</table></dl>\n<p>")
0347         if state==STATE_NBULLET:
0348             doclines.append("</li>")
0349             state = STATE_NBULLET_TRAILING
0350         if state==STATE_NBULLET_TRAILING:
0351             doclines.append("</ol>\n<p>")
0352         if state==STATE_DL or state==STATE_LICENSES:
0353             doclines.append("</dd></dl>")
0354             
0355         doclines.append("</p>")
0356         
0357         newdoc = "<p>" + "\n".join (doclines)
0358         newdoc = newdoc.replace ("<p>\n</p>\n", "")
0359         #newdoc = newdoc.replace ("\n\n", "</p>\n<p>\n")
0360         return handleTags(newdoc)
0361         
0362 if __name__ == '__main__':
0363     r = Reduce()
0364     print(r.do_txt("""
0365 /**
0366 * The base class for configuration modules.
0367 *
0368 * Configuration modules are realized as plugins that are loaded only when
0369 * needed.
0370 *
0371 * The module in principle is a simple widget displaying the
0372 * item to be changed. The module has a very small interface.
0373 *
0374 * All the necessary glue logic and the GUI bells and whistles
0375 * are provided by the control center and must not concern
0376 * the module author.
0377 *
0378 * To write a config module, you have to create a library
0379 * that contains at one factory function like this:
0380 *
0381 * \code
0382 * #include <kgenericfactory.h>
0383 *
0384 * typedef KGenericFactory<YourKCModule, QWidget> YourKCModuleFactory;
0385 * K_EXPORT_COMPONENT_FACTORY( yourLibName, YourKCModuleFactory("name_of_the_po_file") );
0386 * \endcode
0387 *
0388 * @author Matthias Hoelzer-Kluepfel <hoelzer@kde.org>
0389 */
0390 """))