Skip to content

Commit 29d88f2

Browse files
committed
adding a caption block to custom syntax highlighting blocks and giving the example blocks some baseline styling. update ajax content to reflect new example convention (and remove redundant header from content)
1 parent 44abd38 commit 29d88f2

File tree

11 files changed

+156
-175
lines changed

11 files changed

+156
-175
lines changed

Rules

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ class Nanoc3::Filter
2727
languages = LANGUAGES.keys.join("|")
2828

2929
until @string.empty?
30-
match = scan_until /(\+(\S.*?\S?)\+|<(#{languages})(?: filename=["']([^"']*)["'])?>|\z)/m
30+
match = scan_until /(\+(\S.*?\S?)\+|<(#{languages})(?: filename=["']([^"']*)["'])?(?: caption=["']([^"']*)["'])?>|\z)/m
3131

3232
@pending << match.pre_match
3333

3434
if match[2] # +foo+
3535
@pending << "<notextile><tt>#{CGI.escapeHTML(match[2])}</tt></notextile>" if match[2]
3636
elsif match[3] # <language>
3737
flush
38-
generate_brushes match[3], LANGUAGES[match[3]], match[4]
38+
generate_brushes match[3], LANGUAGES[match[3]], match[4], match[5]
3939
end
4040
end
4141

@@ -51,12 +51,14 @@ class Nanoc3::Filter
5151
match
5252
end
5353

54-
def generate_brushes(tag, replace, filename)
54+
def generate_brushes(tag, replace, filename, caption)
5555
match = scan_until %r{</#{tag}>}
56-
@output << %{<div class="code_container">\n}
56+
@output << %{<div class="example">\n}
5757
@output << %{<div class="filename">#{filename}</div>\n} if filename
58-
@output << %{<pre class="brush: #{replace}; gutter: false; toolbar: false">\n} <<
59-
CGI.escapeHTML(match.pre_match) << %{</pre></div>}
58+
@output << %{<pre class="brush: #{replace}; gutter: false; toolbar: false"><code>} <<
59+
CGI.escapeHTML(match.pre_match) << %{</code></pre>}
60+
@output << %{<div class="caption">#{caption}</div>\n} if caption
61+
@output << %{</div>\n}
6062
end
6163

6264
def flush
@@ -96,7 +98,6 @@ end
9698
compile '*' do
9799
filter :code_blocks
98100
filter filters[item[:extension].to_sym] || item[:extension].to_sym
99-
p item.identifier
100101
if item[:homepage]
101102
layout 'home'
102103
elsif item.identifier.match /\/dex\/$/

content/ajax/ajax-and-forms.md

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ section : 4
44
title : Ajax and Forms
55
attribution: jQuery Fundamentals
66
---
7-
## Ajax and Forms
8-
97
jQuery’s ajax capabilities can be especially useful when dealing with forms.
108
The [jQuery Form Plugin](http://jquery.malsup.com/form/) is a well-tested tool
119
for adding Ajax capabilities to forms, and you should generally use it for
@@ -14,20 +12,16 @@ anything remotely complex. That said, there are a two jQuery methods you
1412
should know that relate to form processing in jQuery: `$.fn.serialize` and
1513
`$.fn.serializeArray`.
1614

17-
<div class="example" markdown="1">
18-
Turning form data into a query string
19-
20-
$('#myForm').serialize();
21-
</div>
22-
23-
<div class="example" markdown="1">
24-
Creating an array of objects containing form data
15+
<javascript caption="Turning form data into a query string">
16+
$('#myForm').serialize();
17+
</javascript>
2518

26-
$('#myForm').serializeArray();
19+
<javascript caption="Creating an array of objects containing form data">
20+
$('#myForm').serializeArray();
2721

28-
// creates a structure like this:
29-
[
30-
{ name : 'field1', value : 123 },
31-
{ name : 'field2', value : 'hello world' }
32-
]
33-
</div>
22+
// creates a structure like this:
23+
[
24+
{ name : 'field1', value : 123 },
25+
{ name : 'field2', value : 'hello world' }
26+
]
27+
</javascript>

content/ajax/ajax-events.md

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,14 @@ section : 6
44
title : Ajax Events
55
attribution: jQuery Fundamentals
66
---
7-
## Ajax Events
8-
97
Often, you’ll want to perform an operation whenever an Ajax requests starts or
108
stops, such as showing or hiding a loading indicator. Rather than defining
119
this behavior inside every Ajax request, you can bind Ajax events to elements
1210
just like you'd bind other events. For a complete list of Ajax events, visit
13-
[http://docs.jquery.com/Ajax_Events](http://docs.jquery.com/Ajax_Events "Ajax
14-
Events documentation on docs.jquery.com").
15-
16-
Setting up a loading indicator using Ajax Events
11+
[ Ajax Events documentation on docs.jquery.com ]( http://docs.jquery.com/Ajax_Events ).
1712

18-
<javascript>
13+
<javascript caption="Setting up a loading indicator using Ajax Events">
1914
$('#loading_indicator')
20-
.ajaxStart(function() { $(this).show(); })
21-
.ajaxStop(function() { $(this).hide(); });
15+
.ajaxStart(function() { $(this).show(); })
16+
.ajaxStop(function() { $(this).hide(); });
2217
</javascript>

content/ajax/ajax-excercises.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ section : 7
44
title : Exercises
55
attribution: jQuery Fundamentals
66
---
7-
## Exercises
8-
97
### Load External Content
108

119
Open the file `/exercises/index.html` in your browser. Use the file
@@ -24,11 +22,11 @@ need to leverage the href of that link to get the proper content from
2422
blog.html. Once you have the href, here's one way to process it into an ID
2523
that you can use as a selector in `$.fn.load`:
2624

27-
<div class="example" markdown="1">
28-
var href = 'blog.html#post1';
29-
var tempArray = href.split('#');
30-
var id = '#' + tempArray[1];
31-
</div>
25+
<javascript>
26+
var href = 'blog.html#post1';
27+
var tempArray = href.split('#');
28+
var id = '#' + tempArray[1];
29+
</javascript>
3230

3331
Remember to make liberal use of `console.log` to make sure you're on the right
3432
path!

content/ajax/jquery-ajax-methods.md

Lines changed: 62 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ section : 3
44
title : jQuery's Ajax-Related Methods
55
attribution: jQuery Fundamentals
66
---
7-
## jQuery's Ajax-Related Methods
8-
97
While jQuery does offer many Ajax-related convenience methods, the core
108
`$.ajax` method is at the heart of all of them, and understanding it is
119
imperative. We'll review it first, and then touch briefly on the convenience
@@ -27,44 +25,42 @@ documentation of the configuration options, visit
2725
[http://api.jquery.com/jQuery.ajax/](http://api.jquery.com/jQuery.ajax/ "$.ajax
2826
documentation on api.jquery.com").
2927

30-
<div class="example" markdown="1">
31-
Using the core `$.ajax` method
32-
33-
$.ajax({
34-
// the URL for the request
35-
url : 'post.php',
36-
37-
// the data to send
38-
// (will be converted to a query string)
39-
data : { id : 123 },
40-
41-
// whether this is a POST or GET request
42-
type : 'GET',
43-
44-
// the type of data we expect back
45-
dataType : 'json',
46-
47-
// code to run if the request succeeds;
48-
// the response is passed to the function
49-
success : function(json) {
50-
$('&lt;h1/>').text(json.title).appendTo('body');
51-
$('&lt;div class="content"/>')
52-
.html(json.html).appendTo('body');
53-
},
54-
55-
// code to run if the request fails;
56-
// the raw request and status codes are
57-
// passed to the function
58-
error : function(xhr, status) {
59-
alert('Sorry, there was a problem!');
60-
},
61-
62-
// code to run regardless of success or failure
63-
complete : function(xhr, status) {
64-
alert('The request is complete!');
65-
}
66-
});
67-
</div>
28+
<javascript caption="Using the core `$.ajax` method">
29+
$.ajax({
30+
// the URL for the request
31+
url : 'post.php',
32+
33+
// the data to send
34+
// (will be converted to a query string)
35+
data : { id : 123 },
36+
37+
// whether this is a POST or GET request
38+
type : 'GET',
39+
40+
// the type of data we expect back
41+
dataType : 'json',
42+
43+
// code to run if the request succeeds;
44+
// the response is passed to the function
45+
success : function(json) {
46+
$('&lt;h1/>').text(json.title).appendTo('body');
47+
$('&lt;div class="content"/>')
48+
.html(json.html).appendTo('body');
49+
},
50+
51+
// code to run if the request fails;
52+
// the raw request and status codes are
53+
// passed to the function
54+
error : function(xhr, status) {
55+
alert('Sorry, there was a problem!');
56+
},
57+
58+
// code to run regardless of success or failure
59+
complete : function(xhr, status) {
60+
alert('The request is complete!');
61+
}
62+
});
63+
</javascript>
6864

6965
<div class="note" markdown="1">
7066
### Note
@@ -220,26 +216,24 @@ The type of data you expect back from the server. Optional.
220216
This option is only applicable for methods that don't already specify the data
221217
type in their name. </div>
222218

223-
<div class="example" markdown="1">
224-
Using jQuery's Ajax convenience methods
225-
226-
// get plain text or html
227-
$.get('/users.php', { userId : 1234 }, function(resp) {
228-
console.log(resp);
229-
});
230-
231-
// add a script to the page, then run a function defined in it
232-
$.getScript('/static/js/myScript.js', function() {
233-
functionFromMyScript();
234-
});
235-
236-
// get JSON-formatted data from the server
237-
$.getJSON('/details.php', function(resp) {
238-
$.each(resp, function(k, v) {
239-
console.log(k + ' : ' + v);
240-
});
241-
});
242-
</div>
219+
<javascript caption="Using jQuery's Ajax convenience methods">
220+
// get plain text or html
221+
$.get('/users.php', { userId : 1234 }, function(resp) {
222+
console.log(resp);
223+
});
224+
225+
// add a script to the page, then run a function defined in it
226+
$.getScript('/static/js/myScript.js', function() {
227+
functionFromMyScript();
228+
});
229+
230+
// get JSON-formatted data from the server
231+
$.getJSON('/details.php', function(resp) {
232+
$.each(resp, function(k, v) {
233+
console.log(k + ' : ' + v);
234+
});
235+
});
236+
</javascript>
243237

244238
### `$.fn.load`
245239

@@ -249,16 +243,12 @@ uses the returned HTML to populate the selected element(s). In addition to
249243
providing a URL to the method, you can optionally provide a selector; jQuery
250244
will fetch only the matching content from the returned HTML.
251245

252-
<div class="example" markdown="1">
253-
Using `$.fn.load` to populate an element
246+
<javascript caption="Using `$.fn.load` to populate an element">
247+
$('#newContent').load('/foo.html');
248+
</javascript>
254249

255-
$('#newContent').load('/foo.html');
256-
</div>
257-
258-
<div class="example" markdown="1">
259-
Using `$.fn.load` to populate an element based on a selector
260-
261-
$('#newContent').load('/foo.html #myDiv h1:first', function(html) {
262-
alert('Content updated!');
263-
});
264-
</div>
250+
<javascript caption="Using `$.fn.load` to populate an element based on a selector">
251+
$('#newContent').load('/foo.html #myDiv h1:first', function(html) {
252+
alert('Content updated!');
253+
});
254+
</javascript>

content/ajax/key-concepts.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ section : 2
44
title : Key Concepts
55
attribution: jQuery Fundamentals
66
---
7-
## Key Concepts
8-
97
Proper use of Ajax-related jQuery methods requires understanding some key
108
concepts first.
119

@@ -78,19 +76,19 @@ Ajax calls are asynchronous by default, the response is not immediately
7876
available. Responses can only be handled using a callback. So, for example,
7977
the following code will not work:
8078

81-
<div class="example" markdown="1">
79+
<javascript>
8280
var response;
8381
$.get('foo.php', function(r) { response = r; });
8482
console.log(response); // undefined!
85-
</div>
83+
</javascript>
8684

8785
Instead, we need to pass a callback function to our request; this callback will
8886
run when the request succeeds, at which point we can access the data that it
8987
returned, if any.
9088

91-
<div class="example" markdown="1">
89+
<javascript>
9290
$.get('foo.php', function(response) { console.log(response); });
93-
</div>
91+
</javascript>
9492

9593
### Same-Origin Policy and JSONP
9694

content/ajax/working-with-jsonp.md

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,36 @@ section : 5
44
title : Working with JSONP
55
attribution: jQuery Fundamentals
66
---
7-
## Working with JSONP
8-
97
The advent of JSONP — essentially a consensual cross-site scripting hack — has
108
opened the door to powerful mashups of content. Many prominent sites provide
119
JSONP services, allowing you access to their content via a predefined API. A
1210
particularly great source of JSONP-formatted data is the [Yahoo! Query
1311
Language](http://developer.yahoo.com/yql/console/), which we'll use in the
1412
following example to fetch news about cats.
1513

16-
<div class="example" markdown="1">
17-
Using YQL and JSONP
18-
19-
$.ajax({
20-
url : 'http://query.yahooapis.com/v1/public/yql',
21-
22-
// the name of the callback parameter,
23-
// as specified by the YQL service
24-
jsonp : 'callback',
25-
26-
// tell jQuery we're expecting JSONP
27-
dataType : 'jsonp',
28-
29-
// tell YQL what we want and that we want JSON
30-
data : {
31-
q : 'select title,abstract,url from search.news where query="cat"',
32-
format : 'json'
33-
},
34-
35-
// work with the response
36-
success : function(response) {
37-
console.log(response);
38-
}
39-
});
40-
</div>
14+
<javscript caption="Using YQL and JSONP">
15+
$.ajax({
16+
url : 'http://query.yahooapis.com/v1/public/yql',
17+
18+
// the name of the callback parameter,
19+
// as specified by the YQL service
20+
jsonp : 'callback',
21+
22+
// tell jQuery we're expecting JSONP
23+
dataType : 'jsonp',
24+
25+
// tell YQL what we want and that we want JSON
26+
data : {
27+
q : 'select title,abstract,url from search.news where query="cat"',
28+
format : 'json'
29+
},
30+
31+
// work with the response
32+
success : function(response) {
33+
console.log(response);
34+
}
35+
});
36+
</javscript>
4137

4238
jQuery handles all the complex aspects of JSONP behind-the-scenes — all we have
4339
to do is tell jQuery the name of the JSONP callback parameter specified by YQL

0 commit comments

Comments
 (0)