-
Notifications
You must be signed in to change notification settings - Fork 107
/
bootstrapconverter.html
237 lines (196 loc) · 16.3 KB
/
bootstrapconverter.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap to Min converter</title>
<link rel="stylesheet" href="http://mincss.com/entireframework.min.css" type="text/css">
<style type="text/css">
iframe {
width: 800px;
height: 400px;
}
#convertedHTMLDiv {
max-width: 70%;
margin-left: 15%;
text-align: left;
}
body {
text-align: center;
}
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<script>
//jQuery plugin to change an element's tagname
//http://stackoverflow.com/questions/8584098/how-to-change-an-element-type-using-jquery
(function($) {
$.fn.changeElementType = function(newType) {
if (this[0]) {
var attrs = {};
$.each(this[0].attributes, function(idx, attr) {
attrs[attr.nodeName] = attr.nodeValue;
});
this.replaceWith(function() {
return $("<" + newType + "/>", attrs).append($(this).contents());
});
} else {
console.log('Error - no such element')
}
};
})(jQuery);
</script>
</head>
<body>
<h2>Bootstrap to Min converter</h2>
<textarea id="enteredHTML" placeholder="Enter the body of your HTML page here"></textarea>
<br>
<a onclick="convertHTML(document.getElementById('enteredHTML').value)"><button class="smooth btn btn-a dontconvert">Convert HTML</button></a>
<br>
<a onclick="document.getElementById('enteredHTML').value=bootstrapExamples[0]"><button class="smooth btn btn-b btn-sm dontconvert">Load 1st Bootstrap example</button></a>
<br>
<a onclick="document.getElementById('enteredHTML').value=bootstrapExamples[1]"><button class="smooth btn btn-c btn-sm dontconvert">Load 2nd Bootstrap example</button></a>
<br>
<a onclick="document.getElementById('enteredHTML').value=bootstrapExamples[2]"><button class="smooth btn btn-a btn-sm dontconvert">Load 3rd Bootstrap example</button></a>
<br><br>
<div id="messagesDiv" class="mediumwidth"></div>
<br>
<div id="convertedHTMLDiv">
</div>
<script type="text/javascript">
bootstrapExamples = [
'<div class="navbar navbar-inverse navbar-fixed-top" role="navigation"><div class="container"><div class="navbar-header"><button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"><span class="sr-only">Toggle navigation</span><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span></button><a class="navbar-brand" href="#">Project name</a></div><div class="navbar-collapse collapse"><form class="navbar-form navbar-right" role="form"><div class="form-group"> <input type="text" placeholder="Email" class="form-control"></div> <div class="form-group"> <input type="password" placeholder="Password" class="form-control"></div><button type="submit" class="btn btn-success">Sign in</button></form></div><!--/.navbar-collapse --></div></div><!-- Main jumbotron for a primary marketing message or call to action --><div class="jumbotron"><div class="container"><h1>Hello, world!</h1><p>This is a template for a simple marketing or informational website. It includes a large callout called a jumbotron and three supporting pieces of content. Use it as a starting point to create something more unique.</p><p><a class="btn btn-primary btn-lg" role="button">Learn more »</a></p></div></div><div class="container"><!-- Example row of columns --><div class="row"><div class="col-md-4"><h2>Heading</h2><p>Donec id elit non mi porta gravida at eget metus. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Etiam porta sem malesuada magna mollis euismod. Donec sed odio dui. </p><p><a class="btn btn-default" href="#" role="button">View details »</a></p></div><div class="col-md-4"><h2>Heading</h2><p>Donec id elit non mi porta gravida at eget metus. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Etiam porta sem malesuada magna mollis euismod. Donec sed odio dui. </p><p><a class="btn btn-default" href="#" role="button">View details »</a></p></div><div class="col-md-4"><h2>Heading</h2><p>Donec sed odio dui. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Vestibulum id ligula porta felis euismod semper. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus.</p><p><a class="btn btn-default" href="#" role="button">View details »</a></p></div></div><hr><footer><p>© Company 2013</p></footer></div>',
'<div class="navbar navbar-inverse navbar-fixed-top" role="navigation"><div class="container"><div class="navbar-header"><button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"><span class="sr-only">Toggle navigation</span><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span></button><a class="navbar-brand" href="#">Project name</a></div><div class="collapse navbar-collapse"><ul class="nav navbar-nav"><li class="active"><a href="#">Home</a> </li><li><a href="#about">About</a> </li><li><a href="#contact">Contact</a> </li></ul></div><!--/.nav-collapse --></div></div><div class="container"><div class="starter-template"><h1>Bootstrap starter template</h1><p class="lead">Use this document as a way to quickly start any new project.<br> All you get is this text and a mostly barebones HTML document.</p></div></div><!-- /.container -->',
'<div class="navbar navbar-default navbar-static-top" role="navigation"><div class="container"><div class="navbar-header"><button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"><span class="sr-only">Toggle navigation</span><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span></button><a class="navbar-brand" href="#">Project name</a></div><div class="navbar-collapse collapse"><ul class="nav navbar-nav"><li class="active"><a href="#">Home</a></li><li><a href="#about">About</a></li><li><a href="#contact">Contact</a></li><li class="dropdown"><a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown <b class="caret"></b></a><ul class="dropdown-menu"><li><a href="#">Action</a></li><li><a href="#">Another action</a></li><li><a href="#">Something else</a></li><li class="divider"></li><li class="dropdown-header">Nav header</li><li><a href="#">Separated link</a></li><li><a href="#">One more link</a></li></ul></li></ul><ul class="nav navbar-nav navbar-right"><li><a href="../navbar/">Default</a></li><li class="active"><a href="./">Static top</a></li><li><a href="../navbar-fixed-top/">Fixed top</a></li></ul></div><!--/.nav-collapse --></div></div><div class="container"><!-- Main component for a primary marketing message or call to action --><div class="jumbotron"><h1>Navbar example</h1><p>This example is a quick exercise to illustrate how the default, static and fixed to top navbar work. It includes the responsive CSS and HTML, so it also adapts to your viewport and device.</p><p>To see the difference between static and fixed top navbars, just scroll.</p><p><a class="btn btn-lg btn-primary" href="../../components/#navbar" role="button">View navbar docs »</a></p></div>',
'<nav class="navbar navbar-default" role="navigation"> <div class="container-fluid"> <div class="navbar-header"> <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a class="navbar-brand" href="#">Brand</a> </div><div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> <ul class="nav navbar-nav"> <li class="active"><a href="#">Link</a></li> <li><a href="#">Link</a></li> <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown <span class="caret"></span></a> <ul class="dropdown-menu" role="menu"> <li><a href="#">Action</a></li> <li><a href="#">Another action</a></li> <li><a href="#">Something else here</a></li> <li class="divider"></li> <li><a href="#">Separated link</a></li> <li class="divider"></li> <li><a href="#">One more separated link</a></li> </ul> </li> </ul> <form class="navbar-form navbar-left" role="search"> <div class="form-group"> <input type="text" class="form-control" placeholder="Search"> </div> <button type="submit" class="btn btn-default">Submit</button> </form> <ul class="nav navbar-nav navbar-right"> <li><a href="#">Link</a></li> <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown <span class="caret"></span></a> <ul class="dropdown-menu" role="menu"> <li><a href="#">Action</a></li> <li><a href="#">Another action</a></li> <li><a href="#">Something else here</a></li> <li class="divider"></li> <li><a href="#">Separated link</a></li> </ul> </li> </ul> </div></div></nav>'
]
function moveFromAToB(a, b, which) {
//Moves the inside of an element to the inside of another.
//Takes two strings (for element selectors)
//and an array, which, that says what nth element to select.
if (!which) var which = [0,0]
if ($(a)[which[0]] && $(b)[which[1]]) {
$(b)[which[1]].innerHTML += $(a)[which[0]].innerHTML;
$(a)[which[0]].remove();
}
}
function printMessage(messageText, messageType) {
$('#messagesDiv')[0].innerHTML += "<message class='"+messageType+"'>"+messageText+"</message><br>";
}
function convertHTML(htmlToConvert) {
//Erase previous messages
$('#messagesDiv')[0].innerHTML = "";
convertedHTML = htmlToConvert;
convertedHTMLDiv.innerHTML = convertedHTML;
//First, we convert from Bootstrap's columns to Min's columns.
var columndivs = $('div[class^="col-"]');
for (var i=0; i<columndivs.length; i++) {
columndivs[i].className = columndivs[i].className.replace('col-md-', 'c')
columndivs[i].classList.add('col')
}
//Oh no, we have some weird (not col-md) columns! They need the Bootstrap plugin.
if ($('div[class^="col-"]:not([class^="col-md-"])')[0])
printMessage('Because your page uses Bootstrap grid class prefixes other than col-md, your page requires the Min Bootstrap plugin, which has been included automatically.')
//Next, we convert the navbar
/* Old navbar stuff
if ($('.navbar-header')[0] && $('.navbar-collapse')[0]) moveFromAToB('.navbar-header', '.navbar-collapse')
if ($('.navbar-collapse')[0] && $('.navbar .container')[0]) moveFromAToB('.navbar-collapse', '.navbar .container')
if ($('.navbar .container')[0] && $('.navbar')[0]) moveFromAToB('.navbar .container', '.navbar')
$('.navbar-toggle')[0]) $('.navbar-toggle')[0].remove()
//Dump the navbar forms stuff right into the navbar
//form-group to navbar-form and navbar-form to navbar
var navbarFormItems = $('.form-group');
for (var i=0; i<navbarFormItems.length; i++) {
moveFromAToB('div.form-group','.navbar-form');
}
if ($('.navbar-form')[0] && $('.navbar')[0]) moveFromAToB('.navbar-form', '.navbar');
//Of course, Bootstrap *has* to have two ways to make a navbar
var lisToSquash = $('.nav.navbar-nav li').not('.dropdown').not('ul.dropdown-menu li').not('ul.navbar-right li');
for (var i=0; i<lisToSquash.length; i++) {
moveFromAToB('.navbar .nav.navbar-nav:not(.dropdown-menu) > li[class!="dropdown"]', '.nav.navbar-nav')
}*/
//Convert navbars
var navbars = $('.navbar');
for (var i=0; i<navbars.length; i++) {
$(navbars[i]).addClass('nav').removeClass('navbar')
//Wrap them in a container
navbars[i].innerHTML = '<div class="container">'+navbars[i].innerHTML + '</div>'
}
$('div.nav').changeElementType('nav')
//Delete elements that don't exist in Min
$('.navbar-toggle').remove()
$('.nav .nav > *').unwrap();
$('.navbar-header > *, .container-fluid > *, .navbar-collapse > *').unwrap();
var navbarPluginCSS = '<link rel="stylesheet" href="http://owenversteeg.com/min-navbar-plugin/navbar-plugin.css?200" type="text/css">'
if ($('li.dropdown')[0]) {
//If we have any dropdowns here, we have to include the min-navbar-plugin
convertedHTMLDiv.innerHTML += navbarPluginCSS;
//Let the user know that we've included it
printMessage('Your page requires the navbar plugin, which has been automatically included.<br><br>It adds dropdowns and right-positioned link capabilities to the navbar.', 'warning');
//Now, we need to convert from Bootstrap's weird navbar dropdowns to Min's nice navbar dropdowns
var x = $('li.dropdown > a').length;
for (i=0; i<x; i++) {
$('li.dropdown > a')[0].outerHTML = $('li.dropdown > a')[0].innerHTML;
}
$('.caret').remove(); //we make carets automatically over here in min-land, unlike *some* frameworks I know
//now, we transmogrify all of Bootstrap's elements into Min elements.
$('li.dropdown').changeElementType('div');
$('div.dropdown').addClass('wpd').removeClass('dropdown');
$('ul.dropdown-menu').wrap('<div class="pulldown"></div>')
/*
var x = $('ul.dropdown-menu').length;
for (var i=0; i<x; i++) {
var pulldownDiv = document.createElement('div');
$(pulldownDiv).addClass('pulldown');
var ulInsidePulldownDiv = document.createElement('ul');
pulldownDiv.appendChild(ulInsidePulldownDiv);
$('ul.dropdown-menu')[0].parentElement.appendChild(pulldownDiv);
moveFromAToB('ul.dropdown-menu', 'div.pulldown ul', [i,i]);
}*/
$('ul.dropdown-menu').removeClass('dropdown-menu');
//Of course, Bootstrap *has* to make dropdown headers the unstandard way.
$('li.dropdown-header').removeClass('dropdown-header').changeElementType('h5');
//Given that, what do you think Bootstrap uses to make a divider?
//Yes, that's right, folks: Bootstrap uses a fucking *li* element as a divider. No, not a <hr> or even a <div> - but a <li>. What the hell.
//I'm pretty sure the only thing that could possibly be less semantically correct would be a <canvas>
$('li.divider').removeClass('divider').changeElementType('hr');
}
//Now, we need to check if we have any right-positioned elements
if ($('.navbar-right')[0]) {
//If we don't already have the navbar plugin CSS, include it and let the user know that we did so
if (convertedHTMLDiv.innerHTML.indexOf(navbarPluginCSS) == -1) {
convertedHTMLDiv.innerHTML += navbarPluginCSS;
printMessage('Your page requires the navbar plugin, which has been automatically included.<br><br>It adds dropdowns and right-positioned link capabilities to the navbar.', 'warning');
}
var lisToRighten = $('ul.navbar-right > li[class!="dropdown"]');
for (var i=0; i<lisToRighten.length; i++) {
//Unwrap the <a>s from their oppressive <li> overlords.
moveFromAToB('ul.navbar-right > li[class!="dropdown"]', 'ul.navbar-right');
//Be free, my darlings!
}
//Now, we convert the navbar right <ul> to a navbar right div
$('ul.navbar-right').addClass('navlink-right').removeClass('nav').removeClass('navbar-nav').removeClass('navbar-right').changeElementType('div');
}
//if we have the second way to make a navbar, we need to push the links into the main navbar
if ($('.nav.navbar-nav')[0] && $('.navbar')[0]) moveFromAToB('.nav.navbar-nav', '.navbar')
//Now we make the page name nice and pretty
if ($('.navbar-brand')[0]) $('.navbar-brand').removeClass('navbar-brand').addClass('pagename current')
//Unwrap links
$('.container .container').unwrap()
$('.nav > div > li > *').unwrap();
//Make sure to convert from Bootstrap's link-buttons to Min's normal buttons.
/*
var aButtons = $('a[class^="btn"]');
for (var i=0; i<aButtons.length; i++) {
var newButton = document.createElement('button');
newButton.innerText = aButtons[i].innerText;
aButtons[i].innerText = "";
aButtons[i].appendChild(newButton);
}*/
//Now we make the buttons reasonably sized.
$('.btn').not('[class*="dontconvert"]').not('[class^="btn-lg"]').addClass('btn-sm');
$('.btn-default').removeClass('btn-default')
$('.btn-primary').addClass('btn-a').removeClass('btn-primary')
$('.btn-success').addClass('btn-b').removeClass('btn-success')
$('.btn-danger').addClass('btn-c').removeClass('btn-danger')
}
</script>
</body>
</html>