home *** CD-ROM | disk | FTP | other *** search
/ PC PowerPlay 56 / CDPowerplay56Disc2.iso / demos / blade / data1.cab / Program_Executable_Files / Lib / BHTMLLib.py < prev    next >
Encoding:
Python Source  |  2000-10-27  |  8.8 KB  |  334 lines

  1.  
  2.  
  3. import string
  4.  
  5.  
  6.  
  7. class B_HTMLBase:
  8.   def __init__(self,filename,title):
  9.     self.FileName=filename
  10.     self.Title=title
  11.     self.FileName=filename
  12.     self.HTMLText=[]
  13.  
  14.   def __del__(self):
  15.     pass
  16.  
  17.   def Reset(self):
  18.     self.HTMLText=[]
  19.  
  20.   def WriteHTMLFile(self):
  21.       file=open(self.FileName,"w")
  22.       text=string.joinfields(self.HTMLText)
  23.       file.write(text)
  24.       file.close()
  25.  
  26.   def CanAddTag(self):
  27.     return 0
  28.  
  29.   def CanClose(self):
  30.     return 0
  31.  
  32.   def _GenerateHeader(self):
  33.     Text="<HTML>\n\n"
  34.     Text=Text+"<HEAD>\n"
  35.     Text=Text+"<TITLE>"+self.Title+"</TITLE>\n"
  36.     Text=Text+"<meta name=\"GENERATOR\" content=\"Blade TravelBook\">\n"
  37.     Text=Text+"</HEAD>\n\n"
  38.     return Text
  39.  
  40.   def GenerateHeader(self):
  41.     self.HTMLText.append(self._GenerateHeader())
  42.  
  43.  
  44.  
  45.   def _CloseHTML(self):
  46.     Text="\n</HTML>\n"
  47.     return Text
  48.  
  49.   def CloseHTML(self):
  50.     if not self.CanClose():
  51.       print "Documento no tiene BODY o FRAMESET correcto"
  52.       return
  53.     self.HTMLText.append(self._CloseHTML())
  54.   
  55.  
  56.  
  57.   def _WriteHyperLink(self,text,destiny,target=None):
  58.     Text='<A HREF="'+destiny+'"'
  59.     if target is not None:
  60.       Text=Text+' TARGET="'+target+'" '
  61.     Text=Text+'> '+text+'</A>'
  62.     return Text
  63.  
  64.   def HyperLink(self,text,destiny,target=None):
  65.     if not self.CanAddTag():
  66.       print "HyperLink: Documento no tiene BODY o FRAMESET correcto"
  67.       return
  68.     self.HTMLText.append(self._WriteHyperLink(text,destiny,target))
  69.  
  70.  
  71.  
  72.   def _WriteImage(self,image,descr=None,text=None,align="BOTTOM"):
  73.     Text='<IMG SRC="'+image+'"'
  74.  
  75.     if descr:
  76.       Text=Text+'"  ALT="'+text+'"'
  77.  
  78.     if text:
  79.       Text=Text+'ALIGN='+align
  80.  
  81.     Text=Text+'>'
  82.  
  83.     if text:
  84.       Text=Text+text+'\n'
  85.     return Text
  86.  
  87.   def Image(self,image,descr=None,text=None,align='BOTTOM'):
  88.     if not self.CanAddTag():
  89.       print "Image: Documento no tiene BODY o FRAMESET correcto"
  90.       return
  91.     self.HTMLText.append(self._WriteImage(image,descr,text,align))
  92.  
  93.  
  94.  
  95.  
  96.   def _WriteImageLink(self,image,destiny,descr=None,text=None,align='BOTTOM'):
  97.     Text=self._WriteImage(image,descr,text,align)
  98.     Text=self._WriteHyperLink(Text,destiny)
  99.     return Text
  100.  
  101.   def ImageLink(self,image,destiny,descr=None,text=None,align='BOTTOM'):
  102.     if not self.CanAddTag():
  103.       print "ImageLink: Documento no tiene BODY o FRAMESET correcto"
  104.       return
  105.     self.HTMLText.append(self._WriteImageLink(image,destiny,descr,text,align))
  106.  
  107.  
  108.  
  109.  
  110.   def _WriteParagraph(self,text,align=None):
  111.     Text='<P'
  112.     if align is not None:
  113.       Text=Text+' ALIGN='+align
  114.     Text=Text+'>'+text+'</P>\n'
  115.     return Text
  116.  
  117.   def Paragraph(self,text,align=None):
  118.     if not self.CanAddTag():
  119.       print "Paragraph: Documento no tiene BODY o FRAMESET correcto"
  120.       return
  121.     self.HTMLText.append(self._WriteParagraph(text,align))
  122.  
  123.  
  124.  
  125.   
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132. class B_HTMLDocBody(B_HTMLBase):
  133.   def __init__(self,filename,title):
  134.     B_HTMLBase.__init__(self,filename,title)
  135.     self.HasBody=0 # 0: no tiene, 1: Body empezado, 2: Body cerrado.
  136.  
  137.   def Reset(self):
  138.     B_HTMLBase.Reset(self)
  139.     self.HasBody=0
  140.  
  141.   def _GenerateBody(self,topmargin=None,background=None,scroll="auto",watermark=0):
  142.     Text='<BODY SCROLL="'+scroll+'"'
  143.     if background is not None:
  144.       Text=Text+' BACKGROUND="'+background+'"'
  145.       if watermark:
  146.         Text=Text+' bgproperties="fixed"'
  147.     if topmargin is not None:
  148.       Text=Text+' TOPMARGIN="'+str(topmargin)+'"'
  149.     Text=Text+'>\n\n'
  150.     return Text
  151.  
  152.   def GenerateBody(self,topmargin=None,background=None,scroll="auto",watermark=0):
  153.     if self.HasBody!=0:
  154.       print "Error, Documento ya tiene BODY"
  155.       return
  156.     self.HasBody=1
  157.     self.HTMLText.append(self._GenerateBody(topmargin,background,scroll,watermark))
  158.  
  159.   def _CloseBody(self):
  160.     Text="\n</BODY>\n"
  161.     return Text
  162.  
  163.   def CloseBody(self):
  164.     if self.HasBody!=1:
  165.       print "Error, Documento no tiene BODY inicializado"
  166.       return
  167.     self.HTMLText.append(self._CloseBody())
  168.     self.HasBody=2
  169.  
  170.   def CanAddTag(self):
  171.     return (self.HasBody==1)
  172.  
  173.   def CanClose(self):
  174.     return (self.HasBody==2)
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181. class B_HTMLDocFrames(B_HTMLBase):
  182.   def __init__(self,filename,title):
  183.     B_HTMLBase.__init__(self,filename,title)
  184.     self.HasFrameSet=0 # 0: no tiene, 1: FrameSet empezado, 2: FrameSet cerrado.
  185.     self.nFrames=0
  186.     self.nFramesAdded=0
  187.  
  188.   def Reset(self):
  189.     B_HTMLBase.Reset(self)
  190.     self.HasFrameSet=0 # 0: no tiene, 1: FrameSet empezado, 2: FrameSet cerrado.
  191.     self.nFrames=0
  192.     self.nFramesAdded=0
  193.  
  194.   def _GenerateFrameSet(self,orientation,dimensions,frameborder=None,framespacing=None,border=None):
  195.     Text="<FRAMESET"
  196.     if frameborder is not None:
  197.       Text=Text+" FRAMEBORDER="+str(frameborder)
  198.     if framespacing is not None:
  199.       Text=Text+" FRAMESPACING="+str(framespacing)
  200.     if border is not None:
  201.       Text=Text+" BORDER="+str(border)
  202.     Text=Text+" "+orientation+"=\""
  203.  
  204.     dim_len=len(dimensions)
  205.     dim_counter=0
  206.     for i in dimensions:
  207.       Text=Text+i
  208.       dim_counter=dim_counter+1
  209.       if dim_counter<dim_len: # Si no es la ·ltima, hay que poner una coma.
  210.         Text=Text+", "
  211.     Text=Text+"\">\n"
  212.     self.nFrames=len(dimensions)
  213.     return Text
  214.  
  215.   def GenerateFrameSet(self,orientation,dimensions,frameborder=None,framespacing=None,border=None):
  216.     if self.HasFrameSet!=0:
  217.       print "Error, Documento ya tiene FRAMESET"
  218.       return
  219.     self.HTMLText.append(self._GenerateFrameSet(orientation,dimensions,frameborder,framespacing,border))
  220.     self.HasFrameSet=1
  221.  
  222.   def _CloseFrameSet(self):
  223.     Text='\n</FRAMESET>\n'
  224.     return Text
  225.  
  226.   def CloseFrameSet(self):
  227.     if self.HasFrameSet!=1:
  228.       print "Error, Documento no tiene FRAMESET inicializado"
  229.       return
  230.     if self.nFrames!=self.nFramesAdded:
  231.       for i in range(self.nFramesAdded,self.nFrames):
  232.         self.Frame("ERROR")
  233.  
  234.     self.HTMLText.append(self._CloseFrameSet())
  235.     self.HasFrameSet=2
  236.  
  237.   def CanAddTag(self):
  238.     return (self.HasFrameSet==1)
  239.  
  240.   def CanClose(self):
  241.     return (self.HasFrameSet==2)
  242.  
  243.   def _WriteFrame(self,src,name=None,maginwidth=None,maginheight=None,scrolling=None,noresize=None,frameborder=None):
  244.     if type(src)==type("string"):
  245.       Text='<FRAME SRC="'+src+'" '
  246.     else: # Asume que hereda de HTMLBase
  247.       Text='<FRAME SRC="'+src.FileName+'" '
  248.     if name is not None:
  249.       Text=Text+' NAME="'+name+'"'
  250.     
  251.     if maginwidth is not None:
  252.       Text=Text+' MARGINWIDTH="'+maginwidth+'"'
  253.  
  254.     if maginheight is not None:
  255.       Text=Text+' MARGINHEIGHT="'+maginheight+'"'
  256.  
  257.     if scrolling is not None:
  258.       Text=Text+' SCROLLING="'+scrolling+'"'
  259.  
  260.     if noresize is not None:
  261.       Text=Text+' NORESIZE'
  262.  
  263.     if frameborder is not None and frameborder=="no":
  264.       Text=Text+' FRAMEBORDER="no"'
  265.  
  266.     Text=Text+'> </FRAME>\n'
  267.     return Text
  268.  
  269.   def Frame(self,src,name=None,maginwidth=None,maginheight=None,scrolling=None,noresize=None,frameborder=None):
  270.     if not self.CanAddTag():
  271.       print "Frame: Documento no tiene FRAMESET correcto."
  272.       return
  273.     if self.nFramesAdded<self.nFrames:
  274.       self.HTMLText.append(self._WriteFrame(src,name,maginwidth,maginheight,scrolling,noresize,frameborder))
  275.       self.nFramesAdded=self.nFramesAdded+1
  276.     else:
  277.       print "Error, no se puede a±adir Frame."
  278.  
  279.  
  280. TravelBookLanguage="Spanish"
  281. TravelBookCharacter="Barbarian"
  282.  
  283. def GetEnvValue(val):
  284.   if val=="Language":
  285.     return TravelBookLanguage
  286.   elif val=="Character":
  287.     return TravelBookCharacter
  288.   else:
  289.     print "BHTMLLib.GetEnvValue() -> Error: Unknown environment string.",val
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297. """
  298. if __name__ == '__main__':
  299.   class B_TravelBook(B_HTMLDocBody):
  300.     def __init__(self,filename,title):
  301.       B_HTMLDocBody.__init__(self,filename,title)
  302.       self.GenerateHeader()
  303.       self.GenerateBody()
  304.       self.HyperLink("═ndice","C:/Tmp/TravelBook/Index.htm")
  305.       self.ImageLink("C:/Tmp/fpcreate.gif","C:/Tmp/TravelBook/Index.htm")
  306.       self.Paragraph("Latas de la competencia.","CENTER")
  307.       self.Paragraph(self._WriteHyperLink("Let the sun shine!","C:/"),"CENTER")
  308.       self.CloseBody()
  309.       self.CloseHTML()
  310.  
  311.  
  312.   class B_TravelBookFrame(B_HTMLDocFrames):
  313.     def __init__(self,filename,title):
  314.       B_HTMLDocFrames.__init__(self,filename,title)
  315.       self.GenerateHeader()
  316.       self.GenerateFrameSet("COLS",["20%","80%"])
  317.       self.Frame("c:/tmp/FileName.html")
  318.       self.Frame("c:/tmp/FileName.html")
  319.       self.CloseFrameSet()
  320.       self.CloseHTML()
  321.  
  322.  
  323.   TrBook=B_TravelBook("c:/tmp/FileName.html","TravelBook")
  324.   print TrBook.HTMLText
  325.   TrBook.WriteHTMLFile()
  326.   
  327.   
  328.   TrBookFrame=B_TravelBookFrame("c:/tmp/FileNameFrame.html","TravelBook")
  329.   print TrBookFrame.HTMLText
  330.   TrBookFrame.WriteHTMLFile()
  331. """
  332.  
  333.  
  334.