Advertisement
stuppid_bot

Grid Generator

Jun 3rd, 2014
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.18 KB | None | 0 0
  1. # -*- coding: utf8 -*-
  2. # Screenshot: http://i.gyazo.com/7a6c6125cf35aca2f33cf29219c906f1.png
  3. gutter = 20
  4. grid_types = (2, 3, 4, 5, 8, 10, 12)
  5.  
  6. process_num = lambda n: str(round(n, 2) if n % 1 else int(n))
  7.  
  8. def gen_grid(cols):
  9.     col_width = 100.0 / cols
  10.     out = ''
  11.     for i in range(1, cols):
  12.         width = col_width * i
  13.         out += """
  14.  
  15. .col-{{col}}-{{cols}} {
  16.    width: {{width}}%;
  17. }""".replace('{{col}}', str(i))\
  18.     .replace('{{cols}}', str(cols))\
  19.     .replace('{{width}}', process_num(width))
  20.     return out
  21.  
  22. css = """.container {
  23.    margin: auto;
  24. }
  25.  
  26. .clearfix {
  27.    clear: both;
  28. }
  29.  
  30. .grid {
  31.    overflow: hidden;
  32.    padding: {{gutter}}px 0 0 {{gutter}}px;
  33. }
  34.  
  35. [class*="col-"] {
  36.    float: left;
  37.    box-sizing: border-box;
  38.    padding: 0 {{gutter}}px {{gutter}}px 0;
  39. }""".replace('{{gutter}}', process_num(gutter))
  40. html = """<!doctype html>
  41. <html>
  42.    <head>
  43.        <meta charset="utf8">
  44.        <title>CSS Grid Example</title>
  45.        <link rel="stylesheet" href="grid.css">
  46.        <style type="text/css">
  47.            body {
  48.                margin: 0;
  49.                line-height: 1;
  50.                font-family: Arial, sans-serif;
  51.                font-size: small;
  52.            }
  53.  
  54.            #page {
  55.                width: 980px;
  56.                text-align: center;
  57.            }
  58.  
  59.            .data {
  60.                background: #f3f3f3;
  61.                padding: {{gutter}}px;
  62.                border-radius: {{radius}}px;
  63.            }
  64.        </style>
  65.    </head>
  66.    <body>
  67.        <div id="page" class="container">
  68.            <div class="grid">
  69. """.replace('{{gutter}}', process_num(gutter))\
  70.    .replace('{{radius}}', process_num(gutter / 4))
  71. for n in grid_types:
  72.     css += gen_grid(n)
  73.     for i in range(n):
  74.         html += """                <div class="col-1-{0}">
  75.                    <div class="data">{1}</div>
  76.                </div>
  77. """.format(n, i + 1)
  78.     if n != grid_types[-1]:
  79.         html += '                <br class="clearfix">\n'
  80. html += """            </div>
  81.        </div>
  82.    </body>
  83. </html>"""
  84. with open('grid.css', 'w') as f:
  85.     f.write(css)
  86. with open('grid.htm', 'w') as f:
  87.     f.write(html)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement