Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: snapshot support #70

Merged
merged 8 commits into from
May 29, 2018
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 55 additions & 20 deletions src/wattsi.pas
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,13 @@
IsFirstSearchIndexItem: Boolean = true;

type
TAllVariants = (vHTML, vDEV, vSplit);
TVariants = vHTML..vDEV;
TAllVariants = (vHTML, vDEV, vSnap, vSplit);
TVariants = vHTML..vSnap;

const
kSuffixes: array[TVariants] of UTF8String = ('html', 'dev');
kExcludingAttribute: array[TAllVariants] of UTF8String = ('w-nohtml', 'w-nodev', 'w-nosplit');
kSuffixes: array[TVariants] of UTF8String = ('html', 'dev', 'snap');
kExcludingAttribute: array[TAllVariants] of UTF8String = ('w-nohtml', 'w-nodev', 'w-nosnap', 'w-nosplit');
kDEVAttribute = 'w-dev';
kCrossRefAttribute = 'data-x';
kCrossSpecRefAttribute = 'data-x-href';
kCrossRefInternalLinkAttribute = 'data-x-internal';
Expand Down Expand Up @@ -121,7 +122,7 @@ procedure TFeature.Reset();
Browsers: array[TBrowserIndex] of TBrowser;
Features: TFeatureMap;

procedure ProcessDocument(const Document: TDocument; const Variant: TVariants; out BigTOC: TElement);
procedure ProcessDocument(const Document: TDocument; const Variant: TVariants; out BigTOC: TElement; const SourceGitSHA: AnsiString);
type
PElementListNode = ^TElementListNode;
TElementListNode = record
Expand Down Expand Up @@ -322,7 +323,9 @@ TCrossReferences = record
repeat
if (Current is TElement) then
begin
if (TElement(Current).HasAttribute(kExcludingAttribute[Variant])) then
if ((TElement(Current).HasAttribute(kExcludingAttribute[Variant])) or
((Variant <> vDEV) and
(TElement(Current).HasAttribute(kDEVAttribute)))) then
begin
DropNode();
continue;
Expand Down Expand Up @@ -883,6 +886,15 @@ TCrossReferences = record
end;
end
else
if (Element.IsIdentity(nsHTML, eTitle)) and (Variant = vSnap) then
begin
Element.AppendChild(TText.Create(' (Commit Snapshot '));
Scratch := Default(Rope);
Scratch.Append(@SourceGitSHA[1], Length(SourceGitSHA));
Element.AppendChild(TText.CreateDestructively(Scratch));
Element.AppendChild(TText.Create(')'));
end
else
if (Element.IsIdentity(nsHTML, eDFN)) then
begin
if (Element.HasAttribute(kLTAttribute)) then
Expand Down Expand Up @@ -934,6 +946,18 @@ TCrossReferences = record
Result := False;
end
else
if (Element.isIdentity(nsHTML, eA) and (Element.GetAttribute('class').AsString = 'sha-link') and (Variant = vSnap)) then
begin
Scratch := Default(Rope);
Scratch.Append(@SourceGitSHA[1], Length(SourceGitSHA));
Element.AppendChild(TText.CreateDestructively(Scratch));
Element.AppendChild(TText.Create(' commit'));
Scratch := Default(Rope);
Scratch.Append('https://github.com/whatwg/html/commit/');
Scratch.Append(@SourceGitSHA[1], Length(SourceGitSHA));
Element.SetAttributeDestructively('href', Scratch);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This bit here doesn't work. End up with a bunch of wrong bytes in the final output. Elsewhere I could work around combining an AnsiString with some other type of string, but that doesn't work when setting attributes. I'd appreciate some help.

cc @Hixie @sideshowbarker

end
else
if (Element.isIdentity(nsHTML, eA) and (not Element.HasAttribute(kHrefAttribute))) then
begin
Fail('<a> without href found: ' + Describe(Element));
Expand Down Expand Up @@ -1757,7 +1781,8 @@ procedure Save(const Document: TDocument; const FileName: AnsiString; const InSp
for Variant in TAllVariants do
if (AttributeName = kExcludingAttribute[Variant]) then
Skip := True;
if (Skip or (AttributeName = kCrossRefAttribute) or
if (Skip or (AttributeName = kDEVAttribute) or
(AttributeName = kCrossRefAttribute) or
(AttributeName = kSubDFNAttribute) or
(AttributeName = kCrossSpecRefAttribute) or
(AttributeName = kUndefinedAttribute) or
Expand Down Expand Up @@ -2507,6 +2532,7 @@ function Main(): Boolean;
var
ParamOffset: Integer = 0;
SourceFile: AnsiString;
SourceGitSHA: AnsiString;
Source: TFileData;
Parser: THTMLParser;
BigTOC: TElement;
Expand All @@ -2515,8 +2541,8 @@ function Main(): Boolean;
Variant: TAllVariants;
begin
Result := False;
if (ParamCount() <> 4) then
if ((ParamCount() = 5) and (ParamStr(1) = '--quiet')) then
if (ParamCount() <> 5) then
if ((ParamCount() = 6) and (ParamStr(1) = '--quiet')) then
begin
Quiet := true;
ParamOffset := 1;
Expand All @@ -2531,12 +2557,13 @@ function Main(): Boolean;
begin
Writeln('wattsi: invalid arguments');
Writeln('syntax:');
Writeln(' wattsi [--quiet] <source-file> <output-directory> <caniuse.json> <bugs.csv>');
Writeln(' wattsi [--quiet] <source-file> <source-git-sha> <output-directory> <caniuse.json> <bugs.csv>');
Writeln(' wattsi --version');
exit;
end;
SourceFile := ParamStr(1 + ParamOffset);
OutputDirectory := ParamStr(2 + ParamOffset);
SourceGitSHA := ParamStr(2 + ParamOffset);
OutputDirectory := ParamStr(3 + ParamOffset);
if (not IsEmptyDirectory(OutputDirectory)) then
begin
// only act if, when we start, the output directory is empty, to make sure that the
Expand All @@ -2546,8 +2573,8 @@ function Main(): Boolean;
end;
Features := TFeatureMap.Create(@UTF8StringHash32);
try
PreProcessCanIUseData(ParamStr(3 + ParamOffset));
PreProcessBugsData(ParamStr(4 + ParamOffset));
PreProcessCanIUseData(ParamStr(4 + ParamOffset));
PreProcessBugsData(ParamStr(5 + ParamOffset));
{$IFDEF VERBOSE_PREPROCESSORS}
if (Assigned(Features)) then
for ID in Features do
Expand Down Expand Up @@ -2602,10 +2629,15 @@ function Main(): Boolean;
// gen...
for Variant in TVariants do
begin
MkDir(OutputDirectory + '/multipage-' + kSuffixes[Variant]);
// Create this directory early as ProcessDocument relies on it to store
// /multipage-dev/search-index.json
if (Variant <> vSnap) then
begin
MkDir(OutputDirectory + '/multipage-' + kSuffixes[Variant]);
end;
Inform('Generating ' + Uppercase(kSuffixes[Variant]) + ' variant...');
{$IFDEF TIMINGS} StartTime := Now(); {$ENDIF}
ProcessDocument(Documents[Variant], Variant, BigTOC); // $R-
ProcessDocument(Documents[Variant], Variant, BigTOC, SourceGitSHA); // $R-
{$IFDEF TIMINGS} Writeln('Elapsed time: ', MillisecondsBetween(StartTime, Now()), 'ms'); {$ENDIF}
// output...
if (Variant <> vDEV) then
Expand All @@ -2616,11 +2648,14 @@ function Main(): Boolean;
{$IFDEF TIMINGS} Writeln('Elapsed time: ', MillisecondsBetween(StartTime, Now()), 'ms'); {$ENDIF}
end;
// multipage...
{$IFDEF TIMINGS} Writeln('Splitting spec...'); {$ENDIF}
{$IFDEF TIMINGS} StartTime := Now(); {$ENDIF}
if (not Split(Documents[Variant], BigTOC, OutputDirectory + '/multipage-' + kSuffixes[Variant] + '/')) then
raise EAbort.Create('Could not split specification');
{$IFDEF TIMINGS} Writeln('Elapsed time: ', MillisecondsBetween(StartTime, Now()), 'ms'); {$ENDIF}
if (Variant <> vSnap) then
begin
{$IFDEF TIMINGS} Writeln('Splitting spec...'); {$ENDIF}
{$IFDEF TIMINGS} StartTime := Now(); {$ENDIF}
if (not Split(Documents[Variant], BigTOC, OutputDirectory + '/multipage-' + kSuffixes[Variant] + '/')) then
raise EAbort.Create('Could not split specification');
{$IFDEF TIMINGS} Writeln('Elapsed time: ', MillisecondsBetween(StartTime, Now()), 'ms'); {$ENDIF}
end;
end;
Result := True;
except
Expand Down