From e64ac5df2265b035a819ea448202a2f87fc8257f Mon Sep 17 00:00:00 2001 From: ViViDboarder Date: Tue, 26 Jul 2016 17:21:56 -0700 Subject: [PATCH] Yay! Fixed. Also added some style changes --- .gitignore | 4 ++ Makefile | 16 ++++--- SFDashC/Info-Apex.plist | 2 + SFDashC/Info-Combined.plist | 2 + SFDashC/Info-Pages.plist | 2 + SFDashC/main.go | 89 +++++++++++++++++++++++++++---------- 6 files changed, 86 insertions(+), 29 deletions(-) diff --git a/.gitignore b/.gitignore index 65aac50..798fe87 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,7 @@ *.html Content/ docSet.dsidx +# Downloaded CSS files +docs.min.css +holygrail.min.css +syntax-highlighter.min.css diff --git a/Makefile b/Makefile index b3d368e..8fc0f15 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ default: complete -complete: run-combined package-apex package-vf package-combined +complete: run-combined package-combined clean-index run-apex package-apex clean-index run-vf package-vf run-apex: (cd SFDashC && go run *.go --silent apexcode) @@ -15,8 +15,9 @@ package-apex: $(eval type = Apex) $(eval package = Salesforce $(type).docset) mkdir -p "$(package)/Contents/Resources/Documents" - cp -r SFDashC/atlas.en-us.200.0.apexcode.meta "$(package)/Contents/Resources/Documents/" + cp -r SFDashC/atlas.en-us.apexcode.meta "$(package)/Contents/Resources/Documents/" cp SFDashC/*.html "$(package)/Contents/Resources/Documents/" + cp SFDashC/*.css "$(package)/Contents/Resources/Documents/" cp SFDashC/Info-$(type).plist "$(package)/Contents/Info.plist" cp SFDashC/docSet.dsidx "$(package)/Contents/Resources/" @@ -24,8 +25,9 @@ package-vf: $(eval type = Pages) $(eval package = Salesforce $(type).docset) mkdir -p "$(package)/Contents/Resources/Documents" - cp -r SFDashC/atlas.en-us.200.0.pages.meta "$(package)/Contents/Resources/Documents/" + cp -r SFDashC/atlas.en-us.pages.meta "$(package)/Contents/Resources/Documents/" cp SFDashC/*.html "$(package)/Contents/Resources/Documents/" + cp SFDashC/*.css "$(package)/Contents/Resources/Documents/" cp SFDashC/Info-$(type).plist "$(package)/Contents/Info.plist" cp SFDashC/docSet.dsidx "$(package)/Contents/Resources/" @@ -35,10 +37,14 @@ package-combined: mkdir -p "$(package)/Contents/Resources/Documents" cp -r SFDashC/*.meta "$(package)/Contents/Resources/Documents/" cp SFDashC/*.html "$(package)/Contents/Resources/Documents/" + cp SFDashC/*.css "$(package)/Contents/Resources/Documents/" cp SFDashC/Info-$(type).plist "$(package)/Contents/Info.plist" cp SFDashC/docSet.dsidx "$(package)/Contents/Resources/" -clean: - rm -fr SFDashC/*.meta +clean-index: rm -f SFDashC/docSet.dsidx + +clean: clean-index + rm -fr SFDashC/*.meta + rm -f SFDashC/*.css rm -fr *.docset diff --git a/SFDashC/Info-Apex.plist b/SFDashC/Info-Apex.plist index 887be53..d827904 100644 --- a/SFDashC/Info-Apex.plist +++ b/SFDashC/Info-Apex.plist @@ -12,5 +12,7 @@ dashIndexFilePath apexcode.html + DashDocSetFallbackURL + https://developer.salesforce.com/docs/ diff --git a/SFDashC/Info-Combined.plist b/SFDashC/Info-Combined.plist index fae7ba4..05648b7 100644 --- a/SFDashC/Info-Combined.plist +++ b/SFDashC/Info-Combined.plist @@ -13,5 +13,7 @@ dashIndexFilePath index.htm + DashDocSetFallbackURL + https://developer.salesforce.com/docs/ diff --git a/SFDashC/Info-Pages.plist b/SFDashC/Info-Pages.plist index 0ffa501..8ffc812 100644 --- a/SFDashC/Info-Pages.plist +++ b/SFDashC/Info-Pages.plist @@ -12,5 +12,7 @@ dashIndexFilePath pages.html + DashDocSetFallbackURL + https://developer.salesforce.com/docs/ diff --git a/SFDashC/main.go b/SFDashC/main.go index afcac11..09aac6b 100644 --- a/SFDashC/main.go +++ b/SFDashC/main.go @@ -7,6 +7,7 @@ import ( "fmt" "github.com/coopernurse/gorp" _ "github.com/mattn/go-sqlite3" + "io" "io/ioutil" "net/http" "os" @@ -24,7 +25,7 @@ TODO: // CSS Paths var cssBasePath = "https://developer.salesforce.com/resource/stylesheets" -var cssFiles = []string{"docs.min.css"} +var cssFiles = []string{"holygrail.min.css", "docs.min.css", "syntax-highlighter.min.css"} // JSON Structs @@ -163,7 +164,10 @@ func saveMainContent(toc *AtlasTOC) { ExitIfError(err) defer ofile.Close() - _, err = ofile.WriteString(content) + _, err = ofile.WriteString( + "" + + content, + ) ExitIfError(err) } } @@ -174,6 +178,13 @@ func main() { WithoutWarning() } + // Download CSS + for _, cssFile := range cssFiles { + throttle <- 1 + wg.Add(1) + go downloadCSS(cssFile, &wg) + } + // Init the Sqlite db dbmap = initDb() err := dbmap.TruncateTables() @@ -198,16 +209,6 @@ func main() { printSuccess(toc) } - // Download CSS - throttle <- 1 - /* - * wg.Add(1) - - * for _, cssUrl := range cssFiles { - * go downloadLink(cssBasePath+"/"+cssUrl, &wg) - * } - */ - wg.Wait() } @@ -333,23 +334,25 @@ func (entry TOCEntry) CleanTitle(t SupportedType) string { } // GetRelLink extracts only the relative link from the Link Href -func (entry TOCEntry) GetRelLink() (relLink string) { +func (entry TOCEntry) GetRelLink(removeAnchor bool) (relLink string) { if entry.LinkAttr.Href == "" { return } // Get the JSON file relLink = entry.LinkAttr.Href - anchorIndex := strings.LastIndex(relLink, "#") - if anchorIndex > 0 { - relLink = relLink[0:anchorIndex] + if removeAnchor { + anchorIndex := strings.LastIndex(relLink, "#") + if anchorIndex > 0 { + relLink = relLink[0:anchorIndex] + } } return } // GetContent retrieves Content for this TOCEntry from the API func (entry TOCEntry) GetContent(toc *AtlasTOC) (content *TOCContent, err error) { - relLink := entry.GetRelLink() + relLink := entry.GetRelLink(true) if relLink == "" { return } @@ -371,7 +374,6 @@ func (entry TOCEntry) GetContent(toc *AtlasTOC) (content *TOCContent, err error) // Read the downloaded JSON defer resp.Body.Close() contents, err := ioutil.ReadAll(resp.Body) - // fmt.Println(string(contents)) if err != nil { return } @@ -379,6 +381,13 @@ func (entry TOCEntry) GetContent(toc *AtlasTOC) (content *TOCContent, err error) // Load into Struct content = new(TOCContent) err = json.Unmarshal([]byte(contents), content) + if err != nil { + fmt.Println("Error reading JSON") + fmt.Println(resp.Status) + fmt.Println(url) + fmt.Println(string(contents)) + return + } return } @@ -435,19 +444,19 @@ func processChildReferences(entry TOCEntry, entryType *SupportedType, toc *Atlas } // GetContentFilepath returns the filepath that should be used for the content -func (entry TOCEntry) GetContentFilepath(toc *AtlasTOC) string { - relLink := entry.GetRelLink() +func (entry TOCEntry) GetContentFilepath(toc *AtlasTOC, removeAnchor bool) string { + relLink := entry.GetRelLink(removeAnchor) if relLink == "" { ExitIfError(NewFormatedError("Link not found for %s", entry.ID)) } - return fmt.Sprintf("%s/%s/%s", toc.Version.VersionURL, toc.Deliverable, relLink) + return fmt.Sprintf("atlas.%s.%s.meta/%s/%s", toc.Locale, toc.Deliverable, toc.Deliverable, relLink) } func downloadContent(entry TOCEntry, toc *AtlasTOC, wg *sync.WaitGroup) { defer wg.Done() - filePath := entry.GetContentFilepath(toc) + filePath := entry.GetContentFilepath(toc, true) // Make sure file doesn't exist first if _, err := os.Stat(filePath); os.IsNotExist(err) { content, err := entry.GetContent(toc) @@ -461,13 +470,45 @@ func downloadContent(entry TOCEntry, toc *AtlasTOC, wg *sync.WaitGroup) { ofile, err := os.Create(filePath) ExitIfError(err) + header := "" + + "\n" + for _, cssFile := range cssFiles { + header += fmt.Sprintf("", cssFile) + } + header += "" + defer ofile.Close() - _, err = ofile.WriteString("\n" + content.Content) + _, err = ofile.WriteString( + header + content.Content, + ) ExitIfError(err) } <-throttle } +func downloadCSS(fileName string, wg *sync.WaitGroup) { + defer wg.Done() + + if _, err := os.Stat(fileName); os.IsNotExist(err) { + err = os.MkdirAll(filepath.Dir(fileName), 0755) + ExitIfError(err) + + ofile, err := os.Create(fileName) + ExitIfError(err) + defer ofile.Close() + + cssURL := cssBasePath + "/" + fileName + response, err := http.Get(cssURL) + ExitIfError(err) + defer response.Body.Close() + + _, err = io.Copy(ofile, response.Body) + ExitIfError(err) + } + + <-throttle +} + /********************** Database **********************/ @@ -477,7 +518,7 @@ func saveSearchIndex(dbmap *gorp.DbMap, entry TOCEntry, entryType *SupportedType return } - relLink := entry.GetContentFilepath(toc) + relLink := entry.GetContentFilepath(toc, false) name := entry.CleanTitle(*entryType) if entryType.ShowNamespace && len(entryHierarchy) > 0 { // Show namespace for methods