Skip to content

Commit d1fbda3

Browse files
committed
Allow runnable examples anywhere
1 parent 02a7c29 commit d1fbda3

File tree

4 files changed

+22
-8
lines changed

4 files changed

+22
-8
lines changed

dlang.org.ddoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,8 @@ _=
351351
RELATIVE_LINK2=$(ALOCAL $1, $+)
352352
_=
353353

354+
RUNNABLE_EXAMPLE=$(DIVC runnable-examples, $1)
355+
354356
SAMPLESRC=$(SPANC sample_src, $(AHTTPS github.com/dlang/dmd/blob/master/samples/$0, /dmd/samples/d/$0))
355357
SCINI=$(TC pre, scini notranslate, $0)
356358
SCRIPTLOAD=<script type="text/javascript" src="$1"$+></script>

index.dd

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ $(DIVC intro, $(DIV, $(DIV,
2929
)
3030
)
3131
$(DIVID your-code-here-default,
32+
$(RUNNABLE_EXAMPLE
3233
----
3334
// Round floating point numbers
3435
import std.algorithm, std.conv, std.functional,
@@ -49,6 +50,7 @@ void main()
4950
}
5051
----
5152
)
53+
)
5254
$(EXTRA_EXAMPLE
5355
----
5456
// Sort lines
@@ -418,8 +420,8 @@ Macros:
418420
TAG=<$1>$+</$1>
419421
TAG2=<$1 $2>$3</$1>
420422
D=<span class="d_inlinecode">$0</span>
421-
EXAMPLE=$(TAG2 a, id="a$1-control" class="example-control", )$(TAG2 div, id="a$1" class="example-box", $2)
422-
EXTRA_EXAMPLE=<div class="your-code-here-extra" style="display:none">$0</div>
423+
EXAMPLE=$(TAG2 a, id="a$1-control" class="example-control", )$(TAG2 div, id="a$1" class="example-box", $(RUNNABLE_EXAMPLE $2))
424+
EXTRA_EXAMPLE=<div class="your-code-here-extra" style="display:none">$(RUNNABLE_EXAMPLE $0)</div>
423425
LAYOUT_PREFIX=
424426
LAYOUT_SUFFIX=
425427
$(SCRIPTLOAD $(ROOT_DIR)js/platform-downloads.js, data-latest="$(LATEST)")

js/run.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -335,17 +335,15 @@ $(document).ready(function()
335335

336336
var currentPage = $(location).attr('pathname');
337337

338-
if ($('body')[0].id != "Home")
339-
return;
340-
341-
$('pre[class~=d_code]').each(function(index)
338+
$('.runnable-examples').each(function(index)
342339
{
343-
var stripedText = $(this).text().replace(/\s/gm,'');
340+
var el = $(this).children().first();
341+
var stripedText = el.text().replace(/\s/gm,'');
344342
var md5sum = MD5(stripedText);
345343

346344
var stdin = "";
347345
var args = "";
348-
var currentExample = $(this);
346+
var currentExample = el;
349347
var orig = currentExample.html();
350348

351349
if (typeof mainPage !== 'undefined' && md5sum in mainPage)

spec/module.dd

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ $(H3 $(LNAME2 name_lookup, Symbol Name Lookup))
224224
$(P The simplest form of importing is to just list the
225225
modules being imported:)
226226

227+
$(RUNNABLE_EXAMPLE
227228
---------
228229
module myapp.main;
229230

@@ -239,6 +240,7 @@ class Foo : BaseClass
239240
}
240241
}
241242
---------
243+
)
242244

243245
$(P When a symbol name is used unqualified, a two-phase lookup will happen.
244246
First, the module scope will be searched, starting from the innermost
@@ -383,6 +385,7 @@ $(H3 $(LNAME2 renamed_imports, Renamed Imports))
383385
all references to the module's symbols must be qualified
384386
with:)
385387

388+
$(RUNNABLE_EXAMPLE
386389
---
387390
import io = std.stdio;
388391

@@ -393,6 +396,7 @@ void main()
393396
writeln("hello!"); // error, writeln is undefined
394397
}
395398
---
399+
)
396400

397401
$(P Renamed imports are handy when dealing with
398402
very long import names.)
@@ -402,6 +406,7 @@ $(H3 $(LNAME2 selective_imports, Selective Imports))
402406
$(P Specific symbols can be exclusively imported from
403407
a module and bound into the current namespace:)
404408

409+
$(RUNNABLE_EXAMPLE
405410
---
406411
import std.stdio : writeln, foo = write;
407412

@@ -414,13 +419,15 @@ void main()
414419
fwritefln(stdout, "abc"); // error, fwritefln undefined
415420
}
416421
---
422+
)
417423

418424
$(P $(D static) cannot be used with selective imports.)
419425

420426
$(H3 $(LNAME2 renamed_selective_imports, Renamed and Selective Imports))
421427

422428
$(P When renaming and selective importing are combined:)
423429

430+
$(RUNNABLE_EXAMPLE
424431
------------
425432
import io = std.stdio : foo = writeln;
426433

@@ -437,19 +444,22 @@ void main()
437444
// foo is not a member of io
438445
}
439446
--------------
447+
)
440448

441449

442450
$(H3 $(LNAME2 scoped_imports, Scoped Imports))
443451

444452
$(P Import declarations may be used at any scope. For example:)
445453

454+
$(RUNNABLE_EXAMPLE
446455
--------------
447456
void main()
448457
{
449458
import std.stdio;
450459
writeln("bar");
451460
}
452461
--------------
462+
)
453463

454464
$(P The imports are looked up to satisfy any unresolved symbols at that scope.
455465
Imported symbols may hide symbols from outer scopes.)
@@ -461,6 +471,7 @@ void main()
461471
at function scope cannot be forward referenced.
462472
)
463473

474+
$(RUNNABLE_EXAMPLE
464475
--------------
465476
void main()
466477
{
@@ -477,6 +488,7 @@ void main()
477488
std.stdio.writeln("bar"); // error, std is undefined
478489
}
479490
--------------
491+
)
480492

481493

482494

0 commit comments

Comments
 (0)