Skip to content

Commit d75f135

Browse files
committed
added "multiline code" tag for wysiwyg editor
1 parent b802518 commit d75f135

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

mdx_code_multiline.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from markdown import Extension
2+
from markdown.util import etree
3+
from markdown.inlinepatterns import Pattern
4+
5+
RE = r'\[code\](.*?)\[\/code\]'
6+
7+
8+
class MultilineCodeExtension(Extension):
9+
def extendMarkdown(self, md, md_globals):
10+
element = NestedElements(RE)
11+
md.inlinePatterns.add('pre', element, '<not_strong')
12+
13+
14+
class NestedElements(Pattern):
15+
def handleMatch(self, m):
16+
el1 = etree.Element('pre')
17+
el2 = etree.SubElement(el1, 'cite')
18+
el2.text = m.group(2).strip()
19+
return el1
20+
21+
22+
def makeExtension(configs=None):
23+
return MultilineCodeExtension(configs=configs)

static/js/mdmagick.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function MDM( inputElement ) {
3434

3535
this.activateControls = function( controlsElement ){
3636
var _self = this;
37-
["bold", "italic", "link", "title", "list", "quote", "code", "img", "strike", "gist"].forEach( function( actionName ){
37+
["bold", "italic", "link", "title", "list", "quote", "code", "multilineCode", "img", "strike", "gist"].forEach( function( actionName ){
3838
$( controlsElement ).find( ".mdm-" + actionName ).click( function( event ){ _self.action( actionName, event ) } );
3939
});
4040
};
@@ -152,6 +152,12 @@ MDM.Actions = {
152152
$( inputElement ).replaceSelection( "`" + selection.text + "`" );
153153
},
154154

155+
multilineCode: function( inputElement ){
156+
var selection = $( inputElement ).getSelection();
157+
//TODO: cut all empty lines from selection.text
158+
$( inputElement ).replaceSelection( "[code]\n" + selection.text + "\n[/code]" );
159+
},
160+
155161
img: function( inputElement ){
156162
var link = prompt( "Image URL", "http://" );
157163
if(link) {
@@ -232,6 +238,7 @@ MDM.Utils = {
232238
" <li class=\"mdm-title\"><a tabindex=\"-1\" title=\"height\" class=\"icon-text-height\" href=\"#mdm-title\"><span>T</span></a></li>" +
233239
" <li class=\"mdm-quote\"><a tabindex=\"-1\" title=\"blockquote\" class=\"icon-comment-alt\" href=\"#mdm-quote\"><span>Q</span></a></li>" +
234240
" <li class=\"mdm-code\"><a tabindex=\"-1\" title=\"code\" class=\"icon-code\" href=\"#mdm-code\"><span>C</span></a></li>" +
241+
" <li class=\"mdm-multilineCode\"><a tabindex=\"-1\" title=\"multiline-code\" class=\"icon-terminal\" href=\"#mdm-multilineCode\"><span>MLC</span></a></li>" +
235242
" <li class=\"mdm-list\"><a tabindex=\"-1\" title=\"list\" class=\"icon-list-ul\" href=\"#mdm-list\"><span>l</span></a></li>" +
236243
" <li class=\"mdm-link\"><a tabindex=\"-1\" title=\"link\" class=\"icon-link\" href=\"#mdm-link\"><span>a</span></a></li>" +
237244
" <li class=\"mdm-img\"><a tabindex=\"-1\" title=\"img\" class=\"icon-picture\" href=\"#mdm-img\"><span>Img</span></a></li>" +

web.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from mdx_github_gists import GitHubGistExtension
66
from mdx_strike import StrikeExtension
77
from mdx_quote import QuoteExtension
8+
from mdx_code_multiline import MultilineCodeExtension
89
from werkzeug.contrib.atom import AtomFeed
910
import post
1011
import user
@@ -18,6 +19,7 @@
1819
md.register_extension(GitHubGistExtension)
1920
md.register_extension(StrikeExtension)
2021
md.register_extension(QuoteExtension)
22+
md.register_extension(MultilineCodeExtension)
2123
app.config.from_object('config')
2224

2325

0 commit comments

Comments
 (0)