Package lxml :: Package html :: Module builder
[hide private]
[frames] | no frames]

Source Code for Module lxml.html.builder

  1  """ 
  2  HTML specialisation of ``builder.py`` by Fredrik Lundh 
  3   
  4  Usage:: 
  5   
  6      >>> from lxml.html.builder import * 
  7      >>> html = HTML( 
  8      ...            HEAD( TITLE("Hello World") ), 
  9      ...            BODY( CLASS("main"), 
 10      ...                  H1("Hello World !") 
 11      ...            ) 
 12      ...        ) 
 13   
 14      >>> import lxml.etree 
 15      >>> print lxml.etree.tostring(html, pretty_print=True) 
 16      <html> 
 17        <head> 
 18          <title>Hello World</title> 
 19        </head> 
 20        <body class="main"> 
 21          <h1>Hello World !</h1> 
 22        </body> 
 23      </html> 
 24   
 25  """ 
 26   
 27  from lxml.builder import ElementMaker 
 28  from lxml.html import html_parser 
 29   
 30  E = ElementMaker(makeelement=html_parser.makeelement) 
 31   
 32  # elements 
 33  A = E.a # anchor 
 34  ABBR = E.abbr # abbreviated form (e.g., WWW, HTTP, etc.) 
 35  ACRONYM = E.acronym #  
 36  ADDRESS = E.address # information on author 
 37  APPLET = E.applet # Java applet (DEPRECATED) 
 38  AREA = E.area # client-side image map area 
 39  B = E.b # bold text style 
 40  BASE = E.base # document base URI 
 41  BASEFONT = E.basefont # base font size (DEPRECATED) 
 42  BDO = E.bdo # I18N BiDi over-ride 
 43  BIG = E.big # large text style 
 44  BLOCKQUOTE = E.blockquote # long quotation 
 45  BODY = E.body # document body 
 46  BR = E.br # forced line break 
 47  BUTTON = E.button # push button 
 48  CAPTION = E.caption # table caption 
 49  CENTER = E.center # shorthand for DIV align=center (DEPRECATED) 
 50  CITE = E.cite # citation 
 51  CODE = E.code # computer code fragment 
 52  COL = E.col # table column 
 53  COLGROUP = E.colgroup # table column group 
 54  DD = E.dd # definition description 
 55  DEL = getattr(E, 'del') # deleted text 
 56  DFN = E.dfn # instance definition 
 57  DIR = E.dir # directory list (DEPRECATED) 
 58  DIV = E.div # generic language/style container 
 59  DL = E.dl # definition list 
 60  DT = E.dt # definition term 
 61  EM = E.em # emphasis 
 62  FIELDSET = E.fieldset # form control group 
 63  FONT = E.font # local change to font (DEPRECATED) 
 64  FORM = E.form # interactive form 
 65  FRAME = E.frame # subwindow 
 66  FRAMESET = E.frameset # window subdivision 
 67  H1 = E.h1 # heading 
 68  H2 = E.h2 # heading 
 69  H3 = E.h3 # heading 
 70  H4 = E.h4 # heading 
 71  H5 = E.h5 # heading 
 72  H6 = E.h6 # heading 
 73  HEAD = E.head # document head 
 74  HR = E.hr # horizontal rule 
 75  HTML = E.html # document root element 
 76  I = E.i # italic text style 
 77  IFRAME = E.iframe # inline subwindow 
 78  IMG = E.img # Embedded image 
 79  INPUT = E.input # form control 
 80  INS = E.ins # inserted text 
 81  ISINDEX = E.isindex # single line prompt (DEPRECATED) 
 82  KBD = E.kbd # text to be entered by the user 
 83  LABEL = E.label # form field label text 
 84  LEGEND = E.legend # fieldset legend 
 85  LI = E.li # list item 
 86  LINK = E.link # a media-independent link 
 87  MAP = E.map # client-side image map 
 88  MENU = E.menu # menu list (DEPRECATED) 
 89  META = E.meta # generic metainformation 
 90  NOFRAMES = E.noframes # alternate content container for non frame-based rendering 
 91  NOSCRIPT = E.noscript # alternate content container for non script-based rendering 
 92  OBJECT = E.object # generic embedded object 
 93  OL = E.ol # ordered list 
 94  OPTGROUP = E.optgroup # option group 
 95  OPTION = E.option # selectable choice 
 96  P = E.p # paragraph 
 97  PARAM = E.param # named property value 
 98  PRE = E.pre # preformatted text 
 99  Q = E.q # short inline quotation 
100  S = E.s # strike-through text style (DEPRECATED) 
101  SAMP = E.samp # sample program output, scripts, etc. 
102  SCRIPT = E.script # script statements 
103  SELECT = E.select # option selector 
104  SMALL = E.small # small text style 
105  SPAN = E.span # generic language/style container 
106  STRIKE = E.strike # strike-through text (DEPRECATED) 
107  STRONG = E.strong # strong emphasis 
108  STYLE = E.style # style info 
109  SUB = E.sub # subscript 
110  SUP = E.sup # superscript 
111  TABLE = E.table #  
112  TBODY = E.tbody # table body 
113  TD = E.td # table data cell 
114  TEXTAREA = E.textarea # multi-line text field 
115  TFOOT = E.tfoot # table footer 
116  TH = E.th # table header cell 
117  THEAD = E.thead # table header 
118  TITLE = E.title # document title 
119  TR = E.tr # table row 
120  TT = E.tt # teletype or monospaced text style 
121  U = E.u # underlined text style (DEPRECATED) 
122  UL = E.ul # unordered list 
123  VAR = E.var # instance of a variable or program argument 
124   
125  # attributes (only reserved words are included here) 
126  ATTR = dict 
127 -def CLASS(v): return {'class': v}
128 -def FOR(v): return {'for': v}
129