-
Notifications
You must be signed in to change notification settings - Fork 2
/
rakudoc_draft_3.rakudoc
4214 lines (3291 loc) · 162 KB
/
rakudoc_draft_3.rakudoc
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
=begin rakudoc :kind("Language") :subkind("Language") :category("reference")
=TITLE RakuDoc
=SUBTITLE A Raku slang for documenting Raku software to aid development and use.
=VERSION 2.10.3
RakuDoc is a markup language with simple instructions for simple tasks and more
complex structures to suit larger projects. There is a clear distinction between
documenting intended to help maintain and develop the software, and the visually
presentations needed by a newcomer to understand how to use software.
=begin AUTHORS :hidden
=item Damian Conway (@thoughtstream)
=item Richard Hainsworth (@finanalyst)
=item Elizabeth Mattijen (@lizmat)
=item Aliaksandr Zahatski (@zag)
=end AUTHORS
=head1 SYNOPSIS
=comment should be a semantic block C<=SYNOPSIS>
Consider the two ways in which documentation is used.
=head2 Case 1: You are writing internal documentation to aid software design
=begin code :allow<B> :lang<raku>
unit class Beginners;
B<#| a variable to hold number of people>
has $.participants;
B<#| data that is initially provided by default, but will be overwritten>
has $.new-participant = $=finish;
... # code
B<=finish>
default data string for a new participant
=end code
A RakuDoc compliant editor or Integrated Design Environment will pick up the text following C<#|>
and put it into a pop-up menu whenever you select (e.g. by hovering over) a use of C<$.participants>.
The C<=finish> statement is the last piece of code. Everything after it is treated as a string and placed
in the C<$=finish> variable.
There are other possibilities described below.
=head2 Case2: You are writing external documentation to accompany some software
Consider the following short description:
=begin code :lang<RakuDoc>
=begin rakudoc
=TITLE Tutorial about Flavorizing
=SUBTITLE
A short tutorial on Flavorizing your quarks
=head1 Starting out
In the section you will learn about how to add flavors to quarks produced
by the I<Imagiton>.
It goes without saying that these techniques should B<NOT> be carried out
without a precocious child nearby; they will strain the imagination of
an ossified adult.
=head2 What is a flavour
It is well-known (see the L<Wikipedia article|https://en.wikipedia.org/wiki/Flavour_(particle_physics)>
that quarks N<a quark is a constituent of a hadron> come in six flavors, for example:
=item Up
=item Bottom
=item Charm
Z< Lots more text >
=end rakudoc
=end code
To indicate that all of the markup above is not Raku code, it is enclosed in a block labelled I<rakudoc>.
This is an example of the B<delimited> form of the B<rakudoc> block (so named because it is
specified between C<=begin> and C<=end> directives).
The C<=TITLE>, C<=SUBTITLE>, and C<=SYNOPSIS> are I<semantic blocks> containing standard information that can be searched
by other tools. For all other purposes, they can be considered to the same as C<=head1> I<blocks>.
A C<=head1> block begins a top-level heading. The C<=head2> is a second level heading.
These are examples of the B<abbreviated> form of RakuDoc blocks. Any non-empty text lines
following a heading specifier are collectively treated as the text of the heading.
The C<=item> components are abbreviated blocks with text entries for a list.
There is no need to start or stop a list explicitly; the renderer will place each consecutive
C<=item> in a list. The first non-C<=item> paragraph or block will end the list.
Within the text there are several examples of inline markup instructions, which have the form R<Upper Unicode character> B<< < >> B<< > >>.
In the example above, we used: C<I B N L Z>. These are for Important, Basis, Note (a footnote), Link, and Zero (a comment) markup.
=head1 Introduction
RakuDoc has a variety of B<components> that allow for rich structured documents,
provide markup hints to renderers without assuming an output format, extract
information from compiled programs, provide information to programs, and enable
custom developer extensions.
This document describes a revision of RakuDoc modifying the
original speculation L<S26|https://design.raku.org/S26.html> that was implemented as Raku was developed.
RakuDoc is intended to be backwards compatible with the POD6 markup language based on S26.
RakuDoc is extensible and renderers are free to provide extra functionality beyond the minimum
set of instructions described here.
The markup language is called B<RakuDoc> with the I<CamelCase> spelling in order to highlight
both the I<Raku> and I<Doc> parts for a newcomer. Some authors may prefer B<Rakudoc> with
I<Titlecase> spelling, which is considered a possible but non-canonical variant.
The file extension for a file containing RakuDoc source is B<.rakudoc>.
=begin nested
As it will become apparent, whitespace and indentation plays an important role in RakuDoc.
There are L<quite a few horizontal whitespace characters|https://unicode-explorer.com/articles/space-characters>;
no equivalence is made between them by Rakudo. For example, tabs and spaces are treated as two
separate horizontal whitespace characters, with no assumption that a tab is equivalents, say to
four (or 2, 8, etc as per style manual) spaces.
Consequently, mixing spaces and tabs may B<appear> to create the same visual margin in some
editor, but they may be B<treated> as different margins when parsing RakuDoc.
It is recommended that RakuDoc authors choose a single whitespace character (eg, tab or space)
for margin alignment.
=end nested
=head1 Two use cases for RakuDoc
RakuDoc can be thought of as having components that are closely connected to a program or module
that is being documented (D<code-oriented RakuDoc>), and components that can be used for text-oriented documents, such as the source
for a webpage or a book (D<text-oriented RakuDoc>).
Code-oriented RakuDoc is expected to be "consumed" by users in an editor or Integrated Design Environment (IDE),
while text-oriented RakuDoc is consumed in some rendered version,
such as an HTML web page, a command-line manpage, or a chapter in an e-book.
When RakuDoc components are viewed in an editor or IDE to provide information
about variables, methods, roles, classes and the like, the information could be made available in a pop-up
whenever a documented term is selected in some way (e.g. hovering a mouse over it).
In the editor context, RakuDoc blocks and markup should then be treated as a comment to the code. Editors are
not expected to render the RakuDoc other than to show the RakuDoc components verbatim, but may do
so if it seems expedient.
When RakuDoc components are viewed in a rendered version (for example, as HTML), components related to a running
program should be ignored.
Within the body of a program there may be
several sections of code, which is called the D<ambient context>, interleaved between sections of RakuDoc.
Sections of code that are intended to be examples within the documentation
(i.e. code that is not actual interleaved executable code) can be specified in C<=code> blocks,
which are treated as integral parts of the RakuDoc, not as source code in ambient context.
Consider a source file containing the definition of a class. When the file is edited in an
IDE (e.g. the class is being developed or maintained) the code-oriented RakuDoc is useful
to help the developer understand the internal structure and function of specific elements
within the code.
Furthermore, when the class is imported into another Raku program
the IDE will be able to access the information in declarator blocks attached to specific terms, without
needing the source code to be available.
However, by including text-oriented RakuDoc in the same file, end-user documentation can be
provided for the class within the same source file. By passing the source through a renderer, a
documentation file (e.g. a README.md file for a github repo) can be generated.
This document describes the minimum version of RakuDoc that a renderer or editor must recognise along with
some expected rendering behaviors for text-oriented renderers. I<"Expected behaviors"> means that a renderer
should approximate the behavior as far as possible given the limitations of the output format, and that the
approximation should be a reasonable interpretation of the standard described here.
The RakuDoc design assumes certain types of customisability, such
as the ability to define new blocks or markup instructions. In order to access blocks or functionality not described in this document,
the file containing such RakuDoc instructions should contain a C<use> statement that loads a module that provides
the information needed to render the extensions. The information provided by this module may be renderer-specific.
=head1 Components
RakuDoc has four main types of components, which are distinguished by their scope and by effect they have on other components.
=defn Directives
Directives define how blocks work. Directives have a similar syntax to regular blocks, but they actually operate on other blocks.
New directives cannot be defined. Directives specify B<behaviours> rather than content.
=defn Blocks
Blocks define complete text components, such as a new paragraph or a code sample or a table. New kinds of blocks can be defined.
Blocks contain B<actual content> which is to be directly rendered in some way.
=defn Markup instructions
Markup instructions typically define inline items embedded within a block. Some markup instructions only affect
the visual rendering of their contents.
Others may have side-effects (such as including items into an index or glossary). New markup instructions can be defined.
=defn Metadata options
Options provide information to a block. They may produce side-effects or alter the way a block should be rendered.
A document writer can associate any metadata option with any block or directive. A renderer is only
required to comply with those listed in this document.
=head2 Directive syntax
These are the syntax forms for each directive.
The C<=> of each directive must be the first non-whitespace character on a given line.
=item B<=begin> specifies the start of a delimited block
=item B<=end> specifies the end of a delimited block
=item B<=for> specifies the start of an extended block
=item B<=finish> ends ambient code and is followed by text
=begin item
B<=alias> specifies a text substitution available in subsequent C<A<...>> markup instructions.
The general syntax for an alias directive is:
=begin code :lang<RakuDoc>
=alias ALIAS_NAME Text for substitution
= Optional extra text
=end code
=end item
=begin item
B<=config> specifies default options to be applied to specific types of blocks or markup instructions
within the remainder of the current block-scope. These default options apply from immediately after
the C<=config> directive up to the end of the innermost surrounding block.
The general syntax for configuration directives is:
=begin code :allow< R > :lang<RakuDoc>
=config R<BLOCK_TYPE> R<CONFIG OPTIONS>
= R<OPTIONAL EXTRA CONFIG OPTIONS>
=end code
If the block-type is single uppercase character R<X>,
the C<=config> directive applies the specified configuration options to the R<X> markup instruction.
For example:
=for code :allow<B V> :lang<RakuDoc>
V<=>config B<C> :allow<B I>
This configures the C<C<>> markup instruction to recognize nested C<B<>> and C<I<>> markup instructions
(both of which would otherwise be treated as verbatim within a C<C<>> instruction).
To avoid ambiguities L<Naming rules|#Naming rules> are applied to blocks and markup instructions.
=end item
=begin item
B<=place> specifies a source of information that is to be placed in the current block. The general
syntax of B<=place> is
=begin code :lang<RakuDoc> :allow<R>
=place R<URL> R<OPTIONS>
= R<MORE OPTIONS IF NECESSARY>
=end code
=end item
=head2 Block syntax
Even though visually similar in some contexts, a block is not a directive...nor vice versa.
Blocks may be specified in three ways:
=begin item
B<Delimited form>
A D<delimited block> starts with the directive C<=begin> as the first non-whitespace on a line,
followed by a valid Raku identifier indicating the name of the block, followed optionally by metadata (see "Metadata" below),
followed by content lines. A delimited block is only terminated
by an C<=end> directive followed by the same block name. A delimited block that is not terminated by
an C<=end same-block-name> instruction throws an error.
For example, to specify a C<para> block in delimited form:
=begin code :lang<RakuDoc>
=begin para :meta<data>
This text is the content of the block.
The preceding blank line is also a part of the block,
as are these two non-blank lines.
=end para
=end code
The general syntax is:
=begin code :lang<RakuDoc> :allow<R>
=begin R<BLOCK_TYPE> R<OPTIONAL CONFIG INFO>
= R<OPTIONAL EXTRA CONFIG INFO>
R<BLOCK CONTENTS>
=end R<BLOCK_TYPE>
=end code
=end item
=begin item
B<Extended form>
An D<extended block> begins with the directive C<=for> as the first non-whitespace on a line, followed by the name of the block,
followed by optional metadata. The next non-blank lines after the metadata
specify the content of the block, which is terminated either by the first entirely blank line after the
content or by the first line that begins with a RakuDoc directive. For example, to specify a C<para> block in extended format:
=begin code :lang<RakuDoc>
=for para :meta<data>
This text is the content of the block.
This text is NOT content of the block.
After a blank line, a new block is started.
=end code
The general syntax is:
=begin code :lang<RakuDoc> :allow<R>
=for R<BLOCK_TYPE> R<OPTIONAL CONFIG INFO>
= R<OPTIONAL EXTRA CONFIG INFO>
R<BLOCK DATA>
=end code
=end item
=begin item
B<Abbreviated form>
In an D<abbreviated block>, the name of the block immediately follows an C<=> sign, which must be the first non-whitespace character
on the line. Everything after the block name (up to the first blank line or directive line) is considered the content of the block. For example,
to specify a C<para> block in abbreviated form:
=begin code :lang<RakuDoc>
=para This text is the content of the block.
This text is NOT content of the block.
After a blank line, a new block is started.
=end code
Note that abbreviated blocks cannot be specified with metadata.
Any apparent metadata elements placed after an abbreviated block name
are instead considered to be part of the block contents.
Where an extended or abbreviated block uses a blank line as the block terminator,
the blank line is not included as part of the content of the block.
All subsequent whitespace, whether horizontal or vertical (spaces, tabs, new lines),
is ignored. Note that horizontal and vertical whitespace can be included inside a delimited block,
and may be significant (eg, Verbatim blocks).
=end item
=head2 Markup instruction syntax
The general syntax of a markup instruction is
=for code :lang<RakuDoc>
<Instruction><opening marker><content>|<meta list><closing marker>
=begin item
B«<Instruction>»
This is a single uppercase letter, or a unicode entity with the UPPER property
=end item
=begin item
B«<opening marker>»
This is either one-or-more C«<» characters or a single C<«> character (i.e. The Unicode entity C«E<0x00AB>»)
=end item
=begin item
B«<content>»
This is a string that does not contain the C<|> character (i.e. Unicode entity C«E<0x007C>»), unless that
character is inside a nested markup instruction.
=end item
=begin item
B«<meta list>»
Is a list of strings separated by C<,> or C<;>, depending on the semantics imposed by the <Instruction>
=end item
=begin item
B«<closing marker>»
Is one-or-more C«>» or a single C<»> (i.e. Unicode entity C«E<0x00BB>»).
The closing marker must be symmetrical with the markup instruction's opening marker.
That is, it must consist of the same number of C«>» or C<»> as there were C«<» or C<«>
in the opening marker.
=end item
For example:
=begin code :allow<B> :lang<RakuDoc>
This is B<U<unusual>> text, and this is B<I<important>>.
And this is a B<L<link to the Raku Programming Language|https://raku.org>>.
=end code
Which would be rendered as:
=begin nested
For example: This is U<unusual> text, and this is I<important>.
And this is a L<link to the Raku Programming Language|https://raku.org>.
=end nested
=head2 Metadata syntax
Metadata is specified using a Raku option pair, where the value portion
is always parsed using Raku semantics.
The following table contains some typical examples.
=begin table :!toc :caption('')
Value is... Specify with...
=============== ===================
True :key
False :!key
Literal :key<answer>
List :key("foo", 42)
Hash :key{ :note('Rosebud, they said'), :emphasize }
=end table
Numerical values can be specified in any Raku-compatible format
(42, 42.0, 42e0, 0x2a, 0d42, 0o52, 0b101010). Strings can either be specified
using single or double quotes, or angle brackets (C<'foo'>, C<"bar">, C< <baz> >).
Note that if a string with whitespace is specified in angle brackets, it
is in fact a list of strings: C<< <foo bar baz> >> is the same as C< ('foo','bar','baz') >.
Within the context of the parser of the Raku Programming Language,
it is also possible to refer to any compile-time value inside the meta-data.
Requiring Raku semantics for the value component of a metadata option means
that for compliance to this specification, RakuDoc markup is prohibited
within a metadata option.
For example, C< :bullet( E<BALLOT BOX WITH X> ) > is prohibited. However,
Raku has a syntax for Unicode characters, so C< :bullet«\c[BALLOT BOX WITH CHECK]» >
is possible. Note the C< «...» > syntax which stringifies the contents with
interpolation.
A renderer may provide a mechanism for allowing RakuDoc within a metadata option
but the value part must still conform to Raku semantics, for example,
:caption('This is a heading with I<important markup> in it')
The outer single quotes ensures that the parser passes a plain string to the
renderer, which may then post-process the string.
The metadata of an extended block or abbreviated block may extend beyond the first line declaring the
block. Each subsequent line must start
with an C<=> in the first virtual column, meaning that it must vertically
align with the C<=> of the RakuDoc block declaration,
and it must be followed
by at least one horizontal whitespace character.
For example:
=begin code :lang<RakuDoc>
=for head1 :a-first-line-key<firstvalue> :another-first-line-key<xyz>
= :a-second-line-key(42)
= :a-third-line-key<third>
Content for the header block
=end code
=head1 Directives
Directives are similar in form to I<block> instructions, but they do not
have the extended or delimited forms. If a directive name is preceded by
a C<=begin>, C<=for> or C<=end>, then an error will be thrown.
=head2 Aliases
The C<=alias> directive provides a way to define block-scoped
synonyms for longer RakuDoc sequences, (meta)object declarators from the
code, or even entire chunks of ambient source. These synonyms can then
be inserted into subsequent RakuDoc using the
L<C<A<> formatting code>|#Alias placements>.
An C<=alias> is scoped from immediately after its declaration
up to the end of the innermost surrounding RakuDoc block.
The alias directive takes two arguments. The first is an
identifier (which is usually specified in uppercase, though this is
not mandatory). The second argument consists of one or more
lines of replacement text.
Each C<=alias> directive creates a block-scoped RakuDoc macro that can be invoked during
document generation by placing the identifier (i.e. the first argument
of the alias) in an C<A<>> formatting code. This formatting code is then
replaced by the text returned by the new macro.
The replacement text returned by the alias macro begins at the first
non-whitespace character after the alias's identifier, and continues to
the end of the line. You can extend the replacement text over multiple
lines by starting the following line(s) with an C<=> (at the same level
of indentation as the C<=alias> directive itself) followed by at least
one whitespace. Each additional line of replacement text uses the original
line's (virtual) left margin, as specified by the indentation of the
replacement text on the C<=alias> line.
For example:
=begin code :lang<RakuDoc>
=alias PROGNAME Earl Irradiatem Evermore
=alias VENDOR 4D Kingdoms
=alias TERMS_URLS =item L<http://www.4dk.com/eie>
= =item L<http://www.4dk.co.uk/eie.io/>
= =item L<http://www.fordecay.ch/canttouchthis>
The use of A<PROGNAME> is subject to the terms and conditions
laid out by A<VENDOR>, as specified at:
A<TERMS_URLS>
=end code
This would produce:
=begin nested
=alias PROGNAME Earl Irradiatem Evermore
=alias VENDOR 4D Kingdoms
=alias TERMS_URLS =item L<http://www.4dk.com/eie>
= =item L<http://www.4dk.co.uk/eie.io/>
= =item L<http://www.fordecay.ch/canttouchthis>
The use of A<PROGNAME> is subject to the terms and conditions
laid out by A<VENDOR>, as specified at:
A<TERMS_URLS>
=end nested
The advantage of using aliases is, obviously, that the same alias can be
reused in multiple places in the documentation. Then, if the replacement
text ever has to be changed, it need only be modified in a single place:
=begin code :lang<RakuDoc>
=alias PROGNAME Count Krunchem Constantly
=alias VENDOR Last Chance Receivers Intl
=alias TERMS_URLS L<http://www.c11.com/generic_conditions>
=end code
Alias placement codes may also specify a default display text, before the
alias name and separated from it by a C<|>. When a display is specified,
it will be used if the requested alias cannot be found (and an I<"unknown alias">
warning will be issued in that case):
=begin code :lang<RakuDoc> :allow<B>
The use of A<B<this program |> SOFTWARE> is subject to the terms and conditions
laid out by A<B<our company |> COMPANY>, as specified at:
A<B<(Please visit our website) |> OURTERMS>
=end code
produces ...
=begin nested
The use of A<this program | SOFTWARE> is subject to the terms and conditions
laid out by A<our company | COMPANY>, as specified at:
A<(Please visit our website) | OURTERMS>
=end nested
Furthermore, since none of the aliases were specified in this document, error messages will be produced
by the renderer (perhaps at the end of the rendered page).
=head2 Block specifiers
The C<=begin>, C<=end>, and C<=for> directives all specify types of blocks.
They have already been illustrated in multiple examples and will not be described further here.
=head2 Config
All block forms can be associated with metadata options. Typically the
metadata are specified for a block using the B<extended> or B<delimited>
forms.
If the same option needs to be added to multiple blocks, this can be done using a C<config>
directive, and then the B<abbreviated> block form can be used in the L<same block scope|#Block scope>.
For example, suppose we want all C<=code> blocks to be labeled with a `:delta` within some section of a document
(L<More information on developer notes|#Developer or delta notes>).
Rather than add a C<:delta> modifier to every individual C<=code>, we could configure every
V<=code> block to be automatically given the same developer note, like so:
=begin code :allow< B V > :lang<RakuDoc>
B<<V<=>config code :delta(v2.3.2+, 'oFun enhancements') >>
=code method droll { say 'what a cool operator' }
=code method drool { say 'what a slob' }
=code method drill { say 'keep up' }
=end code
Within the same block scope, eg., between C<=begin section> and C<=end section> instructions,
one or more C<=config> directives can be specified to define the default behaviour of specific block types.
Successive C<=config> directives within the same scope are cumulative in effect.
Because C<=config> directives specify I<default> behaviours, whenever a particular kind of metadata
is explicitly specified for an instance of that block type, that option overrides the defaults set by any active C<=config> blocks.
For example:
=begin code :allow<B>
B<=config item1 :bullet« \c[Earth Globe Europe-Africa] » >
B<=config item2 :bullet« \c[Hand with Index and Middle Fingers Crossed] » >
=comment In the following list, =item1 and =item2 will now have non-standard bullets
The major sources of sustainable energy are:
=item1 geothermal
=item1 fusion
=item2 (eventually)
=begin section
B<=config item1 :toc :headerlevel(2)>
=comment The following items still have non-standard bullets but now they will
also be added to the table of contents as if they were =head2 blocks
Important items are:
=item1 small scale fusion
=item1 room temperature superconductors
=end section
=comment The following list items will NOT be added to the Table of Contents
because the section containing that particular =config has now ended.
They will still have the non-standard Earth bullets though,
because those two =config directives are still in scope.
=item1 matter-antimatter annihilation
=item1 zero-point energy
=end code
When an option needs to be associated with all blocks, the config statement may take a
whatever C<*> instead of a specific block name, like so:
=begin code :lang<RakuDoc>
=config * :!error
=end code
This particular example signals to the renderer that no warning messages should be issued
if errors are encountered by I<any> block type in the current block scope.
The general principle is that more block-specific configurations of metadata take precedence over
more generic configurations. For example, suppose in the same block scope, we have:
=begin code :lang<RakuDoc>
=config * :!error
=config item :error
=for item :!error
Item 1
=for item
Item 2
=for numitem
Item 3
=end code
Then the configuration would applied in the following order:
=numitem V<=>for item V<:!error> (specific metadata applied to a specific block)
=numitem V<=config item :error> (default metadata applied to a specific kind of block)
=numitem V<=config * :!error> (default metadata applied to any kind of block)
Which means that I<Item 1> would never generate a warning (because its C<item> block
is specifically configured not to do so), while I<Item 2> could generate warnings
(because C<item> blocks are generically configured to do so), and I<Item 3> would never
never generate a warning (because generic blocks are generically configured not to do so).
There is more discussion of C<=config> in the context of L<Formatting codes|#Formatting codes>.
=head2 Document termination
The C<=finish> directive indicates the end of all ambient contexts within
the document. This means that the parser will treat all the remaining
text in the file as a string.
Any text after the C<=finish> block can be accessed as a string within the program
(i.e. the ambient code) via the C<$=finish> variable.
This can be used to provide data to a program, such as a test.
=begin code :allow< V B > :lang<raku>
use JSON::Fast;
my %h = from-json( B<$=finish> );
say %h.raku;
# more lines of code
B<V<=>finish
{ "key1": "a string value", "key2": "another value" }>
=end code
This will generate the following output:
=begin code :lang<text>
{:key1("a string value"), :key2("another value")}
=end code
=head2 Table constructors
The C<=row> and C<=column> instructions are directives, not blocks, and they are
described in more detail in the section on the L<procedural C<=table> block|#Procedural description of tables>.
=head2 Placement from external sources
The C<=place> instruction is a directive that tells the renderer to obtain text, information, or an object
from another source.
The rendered version of the data placed in the text is considered to form its own block scope.
An in-line version of C<=place> is C<P<...>>, which is described in more detail in the section on
markup codes.
The schema of the URL specified in a C<=place> directive specifies where to look for the external content:
=begin table :!toc :caption('')
| Schema | Where to look for the resource |
|=============|=================================================|
| C<http(s):> | Look on the web |
| C<file:> | Look on the local filesystem |
| C<rakudoc:> | Look in the usual places for Raku documentation |
| C<man:> | Look via a local man(1) |
| C<defn:> | Look in the current document |
=end table
Meanwhile, the kind of content being placed is inferred from the
final extension of the URL. For example:
=begin table :!toc :caption('')
| Extension | How to treat the contents |
|=============|==================================|
| C<.txt> | Render the contents as plaintext |
| C<.rakudoc> | Render the contents as RakuDoc |
| C<.html> | Render the contents as XHTML |
| C<.md> | Render the contents as Markdown |
| C<.json> | Render the contents as JSON |
| C<.jpg> | Render the contents as an image |
| C<.mp4> | Render the contents as a video |
=end table
Renderers are not required to render anything other than plaintext and RakuDoc,
but may also support other formats if they wish.
In the case where a URL does not end in a recognized extension:
=begin code :lang<RakuDoc>
=place https://example.org/landingpage
=place rakudoc:App::Rak
=place file:/usr/share/legal/std_disclaimer
=end code
...then the type of content (and hence rendering) may be inferred from the schema:
=begin table :!toc :caption('')
| Schema | Inferred content type if no final extension |
|=============|=============================================|
| C<http(s):> | XHTML |
| C<file:> | plaintext |
| C<rakudoc:> | RakuDoc |
=end table
A B<=place> directive may be accompanied by metadata options to ensure that
a reference can be made in the Table of Contents, or to provide an ALT text. For example:
=begin code :lang<RakuDoc>
=place https://raku.org/camelia-logo.png :caption<Raku's mascot> :alt<A multicoloured butterfly>
=end code
will produce ...
=place https://raku.org/camelia-logo.png :caption<Raku's mascot> :alt<A multicoloured butterfly>
=head1 Code-oriented RakuDoc
The following RakuDoc components are primarily intended for use when editing within an editor.
Standalone renderers may ignore them.
X<|Syntax,#|>X<|Syntax,#=>
=head2 Declarator blocks
Declarator blocks differ from other blocks in that they do not have a specific type.
Instead, they are attached to a particular element of the ambient source code.
Declarator blocks are introduced by a special comment: either C<#|> or C<#=>,
which must be immediately followed by either a space or an opening bracket character,
such as a curly brace C<{>, C<(> or C<«>.
If followed by a space, the block is terminated by the end of line;
if followed by one or more opening bracket character, the block may extend over multiple lines and is terminated by
the matching sequence of closing bracket characters.
RakuDoc markup instructions may be used inside declarator blocks.
Blocks starting with C<#|> are attached to the code after them,
and blocks starting with C<#=> are attached to the code before them.
Since declarator blocks are attached to source code, they can be used to
document classes, roles, subroutines and in general any statement or block.
The C<WHY> method can be used on these classes, roles, subroutines, etc. to
return the attached RakuDoc value.
For example:
=begin code :lang<raku>
#| Base class for magicians
class Magician {
has Int $.level;
has Str @.spells;
}
#| Fight mechanics
sub duel( #= Magicians only, no mortals.
Magician $a, #= first magician
Magician $b, #= second magician
) {
}
say Magician.WHY; # OUTPUT: «Base class for magicians»
say &duel.WHY.leading; # OUTPUT: «Fight mechanics»
say &duel.WHY.trailing; # OUTPUT: «Magicians only, no mortals.»
say &duel.signature.params[0].WHY; # OUTPUT: «first magician»
say &duel.signature.params[1].WHY; # OUTPUT: «second magician»
=end code
These declarations can extend to several lines. For example:
=begin code :lang<raku>
#|( This is an example of stringification:
* Numbers turn into strings
* Regexes operate on said strings
* C<with> topicalizes and places result into $_
)
sub search-in-seq
#=« Uses
* topic
* decont operator
»
( Int $end, Int $number ) {
with (^$end).grep( /^$number/ ) {
.say for $_<>;
}
}
=end code
A useful idiom is to place trailing declarator blocks on the parameters of the MAIN sub.
These comments will be picked up automatically by USAGE.
=begin code :lang<raku>
sub MAIN(
Str $first, #= the first CLI parameter
Int :$verbose, #= an integer to indicate output, 0 = no output
)
=end code
=head2 Data blocks
C<=data> blocks are used to specify named and/or ordered chunks of data for the ambient code.
They may appear anywhere within a source file, and as
many times as required.
The corresponding variable, C<$=data> holds an object that does both the Associative
and Positional roles.
Each C<=data> block can be given a C<:key> option, to name it. The contents
of any C<=data> block with a key are accessible (as a single string) via
the Associative aspect of C<$=data> object. For example:
=begin code
=begin data :key<Virtues>
Laziness
Impatience
Hubris
=end data
say 'The three virtues are:';
say $=data<Virtues>;
=end code
The contents of any C<=data> block that does not have a C<:key> are
accessible (as a single string) via the Positional aspect of
C<$=data>. Unkeyed C<=data> blocks are stored in the same order
they appear in the file. For example:
=begin code :lang<raku>
say 'The second anti-virtue is: ', $=data[1];
=data Industry
=data Patience
=data Humility
=end code
Note that, as the preceding example illustrates, because RakuDoc is a
compile-time phenomenon, it is possible to specify C<=data> blocks
I<after> the point in the source where their contents will be used
(provided they're not being used in a C<BEGIN>, of course).
When C<$=data> itself is stringified, it returns the concatenation of all
the unkeyed C<=data> blocks the parser has seen.
C<=data> blocks are never rendered by the text-oriented RakuDoc renderers.
=head1 Text-oriented RakuDoc
The RakuDoc instructions in this section are primarily intended for text-oriented rendering.
However, text-oriented and code-oriented RakuDoc can be freely intermixed with Raku
code in a single file; this may help to create code that is well-documented.
When a Raku compiler parses a file, line contents are expected to be Raku code (ambient text).
When a RakuDoc block is finished, the next line is assumed to revert to ambient text.
=begin code :lang<RakuDoc>
#start of file
my $text; # ambient code
=head this is some text
this line continues the header
#this line is now ambient code and so must be specified as a comment
=end code
A C<rakudoc> block reverses this assumption.
=begin code :lang<RakuDoc>
#start of file
my $text; # ambient code
=begin rakudoc
=head this is some text
this line continues the header
this line is not ambient code and so
it will be treated as an ordinary paragraph
=end rakudoc
# this line is now ambient code
=end code
Files that are intended to contain text-oriented documentation alone should be entirely
enclosed in a C<rakudoc> block.
However, files whose filename ends in a C<.rakudoc> extension are always presumed
to contain only text-oriented documentation, and any renderer must always treat
the file's contents as being enclosed in an implicit C<=begin rakudoc>...C<=end rakudoc>,
unless those contents are already explicitly enclosed in a C<rakudoc> block.
Historically the name C<pod> was used for this functionality.
For compatibility it will still be possible to use the name C<pod>
instead of C<rakudoc> in the foreseeable future. Note however
that the Raku Programming Language may assign slightly different
semantics to C<rakudoc> at some time in the future.
=head2 Headings
Headings can be defined using C<=headN>,
where R<N> is greater than zero (e.g. C<=head1>, C<=head2>, etc.).
C<=head> is an alias for C<=head1>.
=begin code :lang<RakuDoc>
=head1 A top level heading
=head2 A second level heading
=head3 A third level heading
=end code
=head3 Numbered headings
You can specify that a heading is numbered using the C<=numhead> block. For example:
=begin code :lang<RakuDoc>
=numhead1 The Problem
=numhead1 The Solution
=numhead2 Analysis
=head3 Overview
=head3 Details
=numhead2 Design
=numhead1 The Implementation
=end code
which would produce:
=begin code :lang<RakuDoc>
1. The Problem
2. The Solution
2.1. Analysis
Overview
Details
2.2. Design
3. The Implementation
=end code
A document has an I<inherent numbering> for every heading, but only the C<=numheadN> block
makes the numeration visible in the heading and in the Table of Contents.
Note that, even though renderers are not required to distinctly render more than the
first four levels of heading, they are required to correctly honour arbitrarily
nested numberings. That is:
=begin code :lang<RakuDoc>
=numhead6 The Rescue of the Kobayashi Maru
=end code
should produce something like:
=begin code :lang<RakuDoc>
2.3.8.6.1.9. The Rescue of the Kobayashi Maru
=end code
=head2 Block scope
A block scope is important for the C<=config> and C<=alias> directives. In addition,
some renderers may generate secondary pages from primary source files, for example,
gathering together the same method names in multiple classes. It is desirable for all the
relevant blocks to be gathered together.
=head3 Sections
The C<section> block will typically only be used in the delimited form so that
the document writer can define an explicit block scope. The C<=begin section>
declaration starts a new block scope, and the C<=end section> returns the scope to the
previous one. Unlike an C<=end rakudoc>, an C<=end section> does not always change the scope back to ambient code.
It only does so when its corresponding C<=begin section> changed the scope I<from> ambient code.
In contrast, an C<=end rakudoc> always reverts to ambient code.
Sections may be embedded within other sections.
When a C<=begin section> is not present, a renderer may apply the following heuristic to determine
the block scope (assuming X, Y, and Z are digits such that C«0 < X < Y < Z »):
=item A C<=headY> declaration starts a new block scope.
=item The next C<=headY> declaration ends the block scope of the previous C<=headY>
=item A C<=headZ> declaration following a C<=headY> is within the block scope
of the preceding C<=headY> declaration, and does not end it.
=item A C<=headX> declaration ends the scope of any preceding C<=headY> and C<=headZ> declarations.
Note that a C<=begin section> declaration overrides the heuristic, meaning that multiple
C<=headX> declarations can be placed within the same block scope.
A renderer is not expected to render blocks differently when they are embedded in a C<section> block.
Providing metadata to a C<=begin section> block definition is B<not> the same as declaring metadata using a
C<=config> directive. Metadata in a config is provided to all matching blocks in the current block scope. Metadata