snippets / content

All snippets tagged content (2)

  1. replace ^M by new line with vim

    tip to **replace ^M by unix new line ending** that appear on some file. ^ mean _ctrl key_.

    1 :%s/^V^M/\r/g
  2. embed code with markdown and pygments

    This snippet show how to embed in a makrdown content with safe html. It don't use the plugin possibility of markdownd to be more easyly integrated in your code.

      1 # -*- coding: utf-8 -*-
    2 from pygments import highlight, lexers, formatters
    3 from markdown import markdown
    4 import re
    5
    6
    7 lexers_aliases = ['aconf', 'apache', 'apacheconf', 'bash', 'bat', 'bbcode', 'befunge', 'bf', 'boo', 'brainfuck', 'c', 'c#',
    8 'c++', 'cfg', 'cpp', 'csharp', 'css', 'css+django', 'css+erb', 'css+genshi', 'css+genshitext', 'css+jinja',
    9 'css+mako', 'css+myghty', 'css+php', 'css+ruby', 'css+smarty', 'd', 'delphi', 'diff', 'django', 'dylan', 'erb',
    10 'genshi', 'genshitext', 'groff', 'haskell', 'html', 'html+django', 'html+erb', 'html+genshi', 'html+jinja',
    11 'html+kid', 'html+mako', 'html+myghty', 'html+php', 'html+ruby', 'html+smarty', 'ini', 'irb', 'irc', 'java',
    12 'javascript', 'javascript+django', 'javascript+erb', 'javascript+genshi', 'javascript+genshitext', 'javascript+jinja',
    13 'javascript+mako', 'javascript+myghty', 'javascript+php', 'javascript+ruby', 'javascript+smarty', 'jinja', 'js',
    14 'js+django', 'js+erb', 'js+genshi', 'js+genshitext', 'js+jinja', 'js+mako', 'js+myghty', 'js+php', 'js+ruby',
    15 'js+smarty', 'jsp', 'kid', 'latex', 'lua', 'make', 'makefile', 'mako', 'man', 'mf', 'minid', 'moin', 'mupad',
    16 'myghty', 'nroff', 'obj-c', 'objc', 'objective-c', 'objectivec', 'objectpascal', 'ocaml', 'pas', 'pascal', 'perl',
    17 'php', 'php3', 'php4', 'php5', 'pl', 'py', 'pycon', 'pytb', 'python', 'raw', 'rb', 'rbcon', 'redcode', 'rest',
    18 'restructuredtext', 'rhtml', 'rst', 'ruby', 'scheme', 'sh', 'smarty', 'sources.list', 'sourceslist', 'sql', 'tex',
    19 'text', 'trac-wiki', 'vb.net', 'vbnet', 'vim', 'xml', 'xml+django', 'xml+erb', 'xml+genshi', 'xml+jinja', 'xml+kid',
    20 'xml+mako', 'xml+myghty', 'xml+php', 'xml+ruby', 'xml+smarty']
    21
    22 """
    23 We cache lexer aliases to speed parsing. To update them, get lexers_aliases like this :
    24 tlexers=lexers.get_all_lexers()
    25 alexers=[]
    26 for l in tlexers:
    27 for a in l[1]:
    28 alexers.append(a)
    29
    30 alexers.sort()
    31 print alexers
    32 """
    33
    34 content=u"""
    35 test embeded code + *markdown*
    36
    37 {{{
    38 def save(self):
    39 if not self.id:
    40 self.created = datetime.now()
    41 self.updated = datetime.now()
    42 self.content_hilighted = self.hilight()
    43 if self.description:
    44 self.description_html = markdown(self.description, safe_mode=True)
    45
    46 super(Snippet, self).save()
    47 }}}
    48
    49
    50 {{{#!python
    51 def hilight(content):
    52 code = re_code.findall(content)
    53 for c in code:
    54 print c
    55 }}}
    56
    57 <script type="text/javascript" charset="utf-8">
    58 updateTagContent()
    59 </script>
    60
    61
    62 {{{#!c
    63 static int
    64 isAlphanum(int c)
    65 {
    66 return ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') ||
    67 (c >= 'A' && c <= 'Z') || c == '_' || c == '$' || c == '\\' ||
    68 c > 126);
    69 }
    70
    71 }}}
    72
    73 test
    74 """
    75
    76 CODE_TAG_START = "{{{"
    77 CODE_TAG_END = "}}}"
    78
    79 re_code = re.compile('%s(?P<code>.*?)%s' % (re.escape(CODE_TAG_START), re.escape(CODE_TAG_END)), re.DOTALL)
    80
    81 in_tag=False
    82 hilighted=""
    83 lexer=None
    84 for p in re_code.split(content):
    85 if p:
    86 if in_tag:
    87 code_str=p
    88 lang=""
    89
    90 if p.startswith("#!"):
    91 c=""
    92 i=0
    93 while (c!=" " and c !="\n") and i<len(code_str):
    94 lang+=c
    95 c=code_str[i]
    96 i+=1
    97 if len(lang)>2:
    98 lang=lang[2:]
    99 print lang
    100
    101 if lang in lexers_aliases:
    102 lexer=lexers.get_lexer_by_name(lang)
    103 else:
    104 try:
    105 lexer = lexers.guess_lexer(code_str)
    106 except:
    107 lexer = lexers.get_lexer_by_name('text')
    108
    109 print lexer
    110 hilighted+= "<div class=\"hilight\">%s</div>" % highlight(p,lexer,
    111 formatters.HtmlFormatter(linenos='inline',
    112 cssclass="source",
    113 lineseparator="<br />"))
    114 else:
    115 hilighted+=markdown(p, safe_mode=True)
    116 in_tag = not in_tag
    117
    118 print hilighted
showing 10, 25, 50 items per pages

Pages : 1

Flux RSS friendsnippetLatest snippets


More...