notationPages function
text as its pages: each ### Name line starts one. See the library's
comment for what each page's text holds. A text without a heading is one
page called fallback with the id screen, its text as written.
Implementation
NotationPages notationPages(String text, {String fallback = 'Page'}) {
final lines = text.split('\n');
String plain(int i) => lines[i].endsWith('\r') ? lines[i].substring(0, lines[i].length - 1) : lines[i];
final headings = <int>[
for (var i = 0; i < lines.length; i++)
if (_pageHeading.hasMatch(plain(i))) i,
];
if (headings.isEmpty) {
return NotationPages(
source: text,
pages: [NotationPage(name: fallback, id: 'screen', heading: null, from: 1, to: lines.length, text: text)],
headed: false,
shared: 0,
definitions: null,
);
}
// The margin blocks: where each line that starts at the margin begins one,
// and what kind it is.
bool margin(int i) => plain(i).isNotEmpty && !plain(i).startsWith(' ') && !plain(i).startsWith('\t');
bool idle(int i) => plain(i).trim().isEmpty || plain(i).trimLeft().startsWith('//');
// Whether the lines before the first heading are the app's: no line of a
// page among them.
final first = headings.first;
var appOnly = true, any = false;
var inBlock = false;
for (var i = 0; i < first && appOnly; i++) {
if (idle(i)) continue;
any = true;
if (!margin(i)) {
if (!inBlock) appOnly = false;
continue;
}
final l = plain(i);
// What closes a list written over several lines (`]`, `] saved`) is
// its statement's last line.
if (inBlock && RegExp(r'^[\])}]+(\s+saved)?\s*$').hasMatch(l)) continue;
inBlock = _appStatement.hasMatch(l) || _appDefinition.hasMatch(l) || _appOverlay.hasMatch(l);
if (!inBlock) appOnly = false;
}
final shared = appOnly ? first : 0;
// The sections: the lines before the first heading when they are a page,
// then one for each heading.
final sections = <({String name, int? heading, int from, int to})>[
if (!appOnly && any) (name: fallback, heading: null, from: 0, to: first - 1),
for (var h = 0; h < headings.length; h++)
(
name: _pageHeading.firstMatch(plain(headings[h]))![1]!,
heading: headings[h],
from: headings[h] + 1,
to: (h + 1 < headings.length ? headings[h + 1] : lines.length) - 1,
),
];
final ids = <String>{};
String slug(String name) {
var s = name.toLowerCase().replaceAll(RegExp("['’]"), '').replaceAll(RegExp(r'[^a-z0-9]+'), '-').replaceAll(RegExp(r'^-+|-+$'), '');
if (s.isEmpty || !RegExp(r'^[a-z_]').hasMatch(s)) s = 's-$s';
var id = s;
for (var n = 2; !ids.add(id); n++) {
id = '$s-$n';
}
return id;
}
// The state each section declares at the margin: its name, and the lines
// of its declaration (the line and what is indented under it).
final declared = <int, Map<String, List<int>>>{};
for (var s = 0; s < sections.length; s++) {
final own = declared[s] = <String, List<int>>{};
for (var i = sections[s].from; i <= sections[s].to; i++) {
if (!margin(i)) continue;
final m = _declares.firstMatch(plain(i));
if (m == null) continue;
final at = <int>[i];
for (var j = i + 1; j <= sections[s].to && !margin(j) && !idle(j); j++) {
at.add(j);
}
own.putIfAbsent(m[1]!, () => at);
}
}
// The definitions, wherever they are written.
final defining = List<bool>.filled(lines.length, false);
var inDefinition = false;
for (var i = 0; i < lines.length; i++) {
if (headings.contains(i)) {
inDefinition = false;
continue;
}
if (margin(i)) inDefinition = _appDefinition.hasMatch(plain(i));
defining[i] = inDefinition && !idle(i);
}
String blank(int i) => lines[i].endsWith('\r') ? '\r' : '';
String only(bool Function(int i) keep) => [for (var i = 0; i < lines.length; i++) keep(i) ? lines[i] : blank(i)].join('\n');
final pages = <NotationPage>[];
for (var s = 0; s < sections.length; s++) {
final section = sections[s];
final own = [for (var i = section.from; i <= section.to; i++) plain(i)].join('\n');
// The state of another page this one uses without declaring it.
final borrowed = <int>{};
for (var o = 0; o < sections.length; o++) {
if (o == s) continue;
for (final e in declared[o]!.entries) {
if (declared[s]!.containsKey(e.key)) continue;
if (RegExp('(?<![A-Za-z0-9_.])${RegExp.escape(e.key)}(?![A-Za-z0-9_])').hasMatch(own)) borrowed.addAll(e.value);
}
}
pages.add(NotationPage(
name: section.name,
id: slug(section.name),
heading: section.heading == null ? null : section.heading! + 1,
from: section.from + 1,
to: section.to + 1,
text: only((i) => i < shared || (i >= section.from && i <= section.to) || borrowed.contains(i)),
));
}
return NotationPages(source: text, pages: pages, headed: true, shared: shared, definitions: only((i) => defining[i]));
}