* [Vim search plugin with Devdocs in its defaults](https://github.com/waiting-for-dev/vim-www) Just set `let g:www_shortcut_engines = { 'devdocs': ['Devdocs', '<leader>dd'] }` to have a `:Devdocs` command and a `<leader>dd` mapping.
* [Visual Studio Code plugin](https://marketplace.visualstudio.com/items?itemName=akfish.vscode-devdocs ) (1)
* [Visual Studio Code plugin](https://marketplace.visualstudio.com/items?itemName=deibit.devdocs) (2)
Filters which manipulate the Nokogiri node object (`doc` and related methods) are _HTML filters_ and must not manipulate the HTML string (`html`). Vice-versa, filters which manipulate the string representation of the document are _text filters_ and must not manipulate the Nokogiri node object. The two types are divided into two stacks within the scrapers. These stacks are then combined into a pipeline that calls the HTML filters before the text filters (more details [here](https://github.com/Thibaut/devdocs/wiki/Scraper-Reference#filter-stacks)). This is to avoid parsing the document multiple times.
Filters which manipulate the Nokogiri node object (`doc` and related methods) are _HTML filters_ and must not manipulate the HTML string (`html`). Vice-versa, filters which manipulate the string representation of the document are _text filters_ and must not manipulate the Nokogiri node object. The two types are divided into two stacks within the scrapers. These stacks are then combined into a pipeline that calls the HTML filters before the text filters (more details [here](./scraper-reference.md#filter-stacks)). This is to avoid parsing the document multiple times.
The `call` method must return either `doc` or `html`, depending on the type of filter.
## Instance methods
* `doc` [Nokogiri::XML::Node]
The Nokogiri representation of the container element.
* `doc` [Nokogiri::XML::Node]
The Nokogiri representation of the container element.
See [Nokogiri's API docs](http://www.rubydoc.info/github/sparklemotion/nokogiri/Nokogiri/XML/Node) for the list of available methods.
* `html` [String]
* `html` [String]
The string representation of the container element.
* `context` [Hash] **(frozen)**
* `context` [Hash] **(frozen)**
The scraper's `options` along with a few additional keys: `:base_url`, `:root_url`, `:root_page` and `:url`.
* `result` [Hash]
Used to store the page's metadata and pass back information to the scraper.
* `result` [Hash]
Used to store the page's metadata and pass back information to the scraper.
Possible keys:
- `:path` — the page's normalized path
- `:store_path` — the path where the page will be stored (equal to `:path` with `.html` at the end)
- `:internal_urls` — the list of distinct internal URLs found within the page
- `:entries` — the [`Entry`](https://github.com/Thibaut/devdocs/blob/master/lib/docs/core/models/entry.rb) objects to add to the index
Shortcuts for `context[:base_url]`, `context[:url]`, and `context[:root_url]` respectively.
* `root_path` [String]
* `root_path` [String]
Shortcut for `context[:root_path]`.
* `subpath` [String]
The sub-path from the base URL of the current URL.
* `subpath` [String]
The sub-path from the base URL of the current URL.
_Example: if `base_url` equals `example.com/docs` and `current_url` equals `example.com/docs/file?raw`, the returned value is `/file`._
* `slug` [String]
The `subpath` removed of any leading slash or `.html` extension.
* `slug` [String]
The `subpath` removed of any leading slash or `.html` extension.
_Example: if `subpath` equals `/dir/file.html`, the returned value is `dir/file`._
* `root_page?` [Boolean]
* `root_page?` [Boolean]
Returns `true` if the current page is the root page.
* `initial_page?` [Boolean]
* `initial_page?` [Boolean]
Returns `true` if the current page is the root page or its subpath is one of the scraper's `initial_paths`.
## Core filters
@ -148,34 +146,34 @@ The following two models are used under the hood to represent the metadata:
Each scraper must implement its own `EntriesFilter` by subclassing the [`Docs::EntriesFilter`](https://github.com/Thibaut/devdocs/blob/master/lib/docs/filters/core/entries.rb) class. The base class already implements the `call` method and includes four methods which the subclasses can override:
* `get_name` [String]
The name of the default entry (aka. the page's name).
It is usually guessed from the `slug` (documented above) or by searching the HTML markup.
* `get_name` [String]
The name of the default entry (aka. the page's name).
It is usually guessed from the `slug` (documented above) or by searching the HTML markup.
**Default:** modified version of `slug` (underscores are replaced with spaces and forward slashes with dots)
* `get_type` [String]
The type of the default entry (aka. the page's type).
Entries without a type can be searched for but won't be listed in the app's sidebar (unless no other entries have a type).
* `get_type` [String]
The type of the default entry (aka. the page's type).
Entries without a type can be searched for but won't be listed in the app's sidebar (unless no other entries have a type).
**Default:**`nil`
* `include_default_entry?` [Boolean]
Whether to include the default entry.
Used when a page consists of multiple entries (returned by `additional_entries`) but doesn't have a name/type of its own, or to remove a page from the index (if it has no additional entries), in which case it won't be copied on the local filesystem and any link to it in the other pages will be broken (as explained on the [Scraper Reference](https://github.com/Thibaut/devdocs/wiki/Scraper-Reference) page, this is used to keep the `:skip` / `:skip_patterns` options to a maintainable size, or if the page includes links that can't reached from anywhere else).
* `include_default_entry?` [Boolean]
Whether to include the default entry.
Used when a page consists of multiple entries (returned by `additional_entries`) but doesn't have a name/type of its own, or to remove a page from the index (if it has no additional entries), in which case it won't be copied on the local filesystem and any link to it in the other pages will be broken (as explained on the [Scraper Reference](./scraper-reference.md) page, this is used to keep the `:skip` / `:skip_patterns` options to a maintainable size, or if the page includes links that can't reached from anywhere else).
**Default:**`true`
* `additional_entries` [Array]
The list of additional entries.
Each entry is represented by an Array of three attributes: its name, fragment identifier, and type. The fragment identifier refers to the `id` attribute of the HTML element (usually a heading) that the entry relates to. It is combined with the page's path to become the entry's path. If absent or `nil`, the page's path is used. If the type is absent or `nil`, the default `type` is used.
Example: `[ ['One'], ['Two', 'id'], ['Three', nil, 'type'] ]` adds three additional entries, the first one named "One" with the default path and type, the second one named "Two" with the URL fragment "#id" and the default type, and the third one named "Three" with the default path and the type "type".
The list is usually constructed by running through the markup. Exceptions can also be hard-coded for specific pages.
* `additional_entries` [Array]
The list of additional entries.
Each entry is represented by an Array of three attributes: its name, fragment identifier, and type. The fragment identifier refers to the `id` attribute of the HTML element (usually a heading) that the entry relates to. It is combined with the page's path to become the entry's path. If absent or `nil`, the page's path is used. If the type is absent or `nil`, the default `type` is used.
Example: `[ ['One'], ['Two', 'id'], ['Three', nil, 'type'] ]` adds three additional entries, the first one named "One" with the default path and type, the second one named "Two" with the URL fragment "#id" and the default type, and the third one named "Three" with the default path and the type "type".
The list is usually constructed by running through the markup. Exceptions can also be hard-coded for specific pages.
**Default:**`[]`
The following accessors are also available, but must not be overridden:
* `name` [String]
* `name` [String]
Memoized version of `get_name` (`nil` for the root page).
* `type` [String]
* `type` [String]
Memoized version of `get_type` (`nil` for the root page).
* [Vim search plugin with Devdocs in its defaults](https://github.com/waiting-for-dev/vim-www) Just set `let g:www_shortcut_engines = { 'devdocs': ['Devdocs', '<leader>dd'] }` to have a `:Devdocs` command and a `<leader>dd` mapping.
* [Visual Studio Code plugin](https://marketplace.visualstudio.com/items?itemName=akfish.vscode-devdocs ) (1)
* [Visual Studio Code plugin](https://marketplace.visualstudio.com/items?itemName=deibit.devdocs) (2)
@ -34,51 +32,51 @@ Each URL is requested only once (case-insensitive).
Configuration is done via class attributes and divided into three main categories:
* [Attributes](https://github.com/Thibaut/devdocs/wiki/Scraper-Reference#attributes) — essential information such as name, version, URL, etc.
* [Filter stacks](https://github.com/Thibaut/devdocs/wiki/Scraper-Reference#filter-stacks) — the list of filters that will be applied to each page.
* [Filter options](https://github.com/Thibaut/devdocs/wiki/Scraper-Reference#filter-options) — the options passed to said filters.
* [Attributes](#attributes) — essential information such as name, version, URL, etc.
* [Filter stacks](#filter-stacks) — the list of filters that will be applied to each page.
* [Filter options](#filter-options) — the options passed to said filters.
**Note:** scrapers are located in the [`lib/docs/scrapers`](https://github.com/Thibaut/devdocs/tree/master/lib/docs/scrapers/) directory. The class's name must be the [CamelCase](http://api.rubyonrails.org/classes/String.html#method-i-camelize) equivalent of the filename.
### Attributes
* `name` [String]
Must be unique.
* `name` [String]
Must be unique.
Defaults to the class's name.
* `slug` [String]
Must be unique, lowercase, and not include dashes (underscores are ok).
* `slug` [String]
Must be unique, lowercase, and not include dashes (underscores are ok).
Defaults to `name` lowercased.
* `type` [String] **(required, inherited)**
Defines the CSS class name (`_[type]`) and custom JavaScript class (`app.views.[Type]Page`) that will be added/loaded on each page. Documentations sharing a similar structure (e.g. generated with the same tool or originating from the same website) should use the same `type` to avoid duplicating the CSS and JS.
* `type` [String] **(required, inherited)**
Defines the CSS class name (`_[type]`) and custom JavaScript class (`app.views.[Type]Page`) that will be added/loaded on each page. Documentations sharing a similar structure (e.g. generated with the same tool or originating from the same website) should use the same `type` to avoid duplicating the CSS and JS.
Must include lowercase letters only.
* `release` [String] **(required)**
* `release` [String] **(required)**
The version of the software at the time the scraper was last run. This is only informational and doesn't affect the scraper's behavior.
* `base_url` [String] **(required in `UrlScraper`)**
The documents' location. Only URLs _inside_ the `base_url` will be scraped. "inside" more or less means "starting with" except that `/docs` is outside `/doc` (but `/doc/` is inside).
Defaults to `localhost` in `FileScraper`. _(Note: any iframe, image, or skipped link pointing to localhost will be removed by the `CleanLocalUrls` filter; the value should be overridden if the documents are available online.)_
* `base_url` [String] **(required in `UrlScraper`)**
The documents' location. Only URLs _inside_ the `base_url` will be scraped. "inside" more or less means "starting with" except that `/docs` is outside `/doc` (but `/doc/` is inside).
Defaults to `localhost` in `FileScraper`. _(Note: any iframe, image, or skipped link pointing to localhost will be removed by the `CleanLocalUrls` filter; the value should be overridden if the documents are available online.)_
Unless `root_path` is set, the root/initial URL is equal to `base_url`.
* `root_path` [String] **(inherited)**
* `root_path` [String] **(inherited)**
The path from the `base_url` of the root URL.
* `initial_paths` [Array] **(inherited)**
A list of paths (from the `base_url`) to add to the initial queue. Useful for scraping isolated documents.
* `initial_paths` [Array] **(inherited)**
A list of paths (from the `base_url`) to add to the initial queue. Useful for scraping isolated documents.
Defaults to `[]`. _(Note: the `root_path` is added to the array at runtime.)_
The absolute path where the files are located on the local filesystem.
_Note: `FileScraper` works exactly like `UrlScraper` (manipulating the same kind of URLs) except that it substitutes `base_url` with `dir` in order to read files instead of making HTTP requests._
Query string parameters to append to every URL. (e.g. `{ format: 'raw' }` → `?format=raw`)
Defaults to `{}`.
* `abstract` [Boolean]
Make the scraper abstract / not runnable. Used for sharing behavior with other scraper classes (e.g. all MDN scrapers inherit from the abstract [`Mdn`](https://github.com/Thibaut/devdocs/blob/master/lib/docs/scrapers/mdn/mdn.rb) class).
* `abstract` [Boolean]
Make the scraper abstract / not runnable. Used for sharing behavior with other scraper classes (e.g. all MDN scrapers inherit from the abstract [`Mdn`](https://github.com/Thibaut/devdocs/blob/master/lib/docs/scrapers/mdn/mdn.rb) class).
Defaults to `false`.
### Filter stacks
@ -122,67 +120,67 @@ Additionally:
The filter options are stored in the `options` Hash. The Hash is inheritable (a recursive copy) and empty by default.
More information about how filters work is available on the [Filter Reference](https://github.com/Thibaut/devdocs/wiki/Filter-Reference) page.
More information about how filters work is available on the [Filter Reference](./filter-reference.md) page.
A CSS selector of the container element. Everything outside of it will be removed and become unavailable to the other filters. If more than one element match the selector, the first one inside the DOM is used. If no elements match the selector, an error is raised.
If the value is a Proc, it is called for each page with the filter instance as argument, and should return a selector or `nil`.
The default container is the `<body>` element.
_Note: links outside of the container element will not be followed by the scraper. To remove links that should be followed, use a [`CleanHtml`](https://github.com/Thibaut/devdocs/wiki/Filter-Reference#cleanhtmlfilter) filter later in the stack._
- `:container` [String or Proc]
A CSS selector of the container element. Everything outside of it will be removed and become unavailable to the other filters. If more than one element match the selector, the first one inside the DOM is used. If no elements match the selector, an error is raised.
If the value is a Proc, it is called for each page with the filter instance as argument, and should return a selector or `nil`.
The default container is the `<body>` element.
_Note: links outside of the container element will not be followed by the scraper. To remove links that should be followed, use a [`CleanHtml`](./filter-reference.md#cleanhtmlfilter) filter later in the stack._
The following options are used to modify URLs in the pages. They are useful to remove duplicates (when the same page is accessible from multiple URLs) and fix websites that have a bunch of redirections in place (when URLs that should be scraped, aren't, because they are behind a redirection which is outside of the `base_url` — see the MDN scrapers for examples of this).
- `:replace_urls` [Hash]
Replaces all instances of a URL with another.
- `:replace_urls` [Hash]
Replaces all instances of a URL with another.
Format: `{ 'original_url' => 'new_url' }`
- `:replace_paths` [Hash]
Replaces all instances of a sub-path (path from the `base_url`) with another.
- `:replace_paths` [Hash]
Replaces all instances of a sub-path (path from the `base_url`) with another.
Format: `{ 'original_path' => 'new_path' }`
- `:fix_urls` [Proc]
- `:fix_urls` [Proc]
Called with each URL. If the returned value is `nil`, the URL isn't modified. Otherwise the returned value is used as replacement.
_Note: before these rules are applied, all URLs are converted to their fully qualified counterpart (http://...)._
Internal URLs are the ones _inside_ the scraper's `base_url` ("inside" more or less means "starting with", except that `/docs` is outside `/doc`). They will be scraped unless excluded by one of the following rules. All internal URLs are converted to relative URLs inside the pages.
- `:skip_links` [Boolean or Proc]
If `false`, does not convert or follow any internal URL (creating a single-page documentation).
- `:skip_links` [Boolean or Proc]
If `false`, does not convert or follow any internal URL (creating a single-page documentation).
If the value is a Proc, it is called for each page with the filter instance as argument.
- `:follow_links` [Proc]
- `:follow_links` [Proc]
Called for page with the filter instance as argument. If the returned value is `false`, does not add internal URLs to the queue.
- `:trailing_slash` [Boolean]
If `true`, adds a trailing slash to all internal URLs. If `false`, removes it.
- `:trailing_slash` [Boolean]
If `true`, adds a trailing slash to all internal URLs. If `false`, removes it.
This is another option used to remove duplicate pages.
- `:skip` [Array]
- `:skip` [Array]
Ignores internal URLs whose sub-paths (path from the `base_url`) are in the Array (case-insensitive).
- `:skip_patterns` [Array]
- `:skip_patterns` [Array]
Ignores internal URLs whose sub-paths match any Regexp in the Array.
- `:only` [Array]
- `:only` [Array]
Ignores internal URLs whose sub-paths aren't in the Array (case-insensitive) and don't match any Regexp in `:only_patterns`.
- `:only_patterns` [Array]
- `:only_patterns` [Array]
Ignores internal URLs whose sub-paths don't match any Regexp in the Array and aren't in `:only`.
If the scraper has a `root_path`, the empty and `/` paths are automatically skipped.
If the scraper has a `root_path`, the empty and `/` paths are automatically skipped.
If `:only` or `:only_patterns` is set, the root path is automatically added to `:only`.
_Note: pages can be excluded from the index based on their content using the [`Entries`](https://github.com/Thibaut/devdocs/wiki/Filter-Reference#entriesfilter) filter. However, their URLs will still be converted to relative in the other pages and trying to open them will return a 404 error. Although not ideal, this is often better than having to maintain a long list of `:skip` URLs._
_Note: pages can be excluded from the index based on their content using the [`Entries`](./filter-reference.md#entriesfilter) filter. However, their URLs will still be converted to relative in the other pages and trying to open them will return a 404 error. Although not ideal, this is often better than having to maintain a long list of `:skip` URLs._
Unless the value is `false`, adds a title to every page.
If the value is `nil`, the title is the name of the page as determined by the [`Entries`](https://github.com/Thibaut/devdocs/wiki/Filter-Reference#entriesfilter) filter. Otherwise the title is the String or the value returned by the Proc (called for each page, with the filter instance as argument). If the Proc returns `nil` or `false`, no title is added.
- `:root_title` [String or Boolean]
- `:title` [String or Boolean or Proc]
Unless the value is `false`, adds a title to every page.
If the value is `nil`, the title is the name of the page as determined by the [`Entries`](./filter-reference.md#entriesfilter) filter. Otherwise the title is the String or the value returned by the Proc (called for each page, with the filter instance as argument). If the Proc returns `nil` or `false`, no title is added.
- `:root_title` [String or Boolean]
Overrides the `:title` option for the root page only.
Adding a documentation may look like a daunting task but once you get the hang of it, it's actually quite simple. Don't hesitate to ask for help on the [mailing list](https://groups.google.com/d/forum/devdocs) if you ever get stuck.
Adding a documentation may look like a daunting task but once you get the hang of it, it's actually quite simple. Don't hesitate to ask for help [in Gitter](https://gitter.im/FreeCodeCamp/DevDocs) if you ever get stuck.
**Note:** please read the [contributing guidelines](https://github.com/Thibaut/devdocs/blob/master/.github/CONTRIBUTING.md) before submitting a new documentation.
1. Create a subclass of `Docs::UrlScraper` or `Docs::FileScraper` in the `lib/docs/scrapers/` directory. Its name should be the [PascalCase](http://api.rubyonrails.org/classes/String.html#method-i-camelize) equivalent of the filename (e.g. `my_doc` → `MyDoc`)
2. Add the appropriate class attributes and filter options (see the [Scraper Reference](https://github.com/Thibaut/devdocs/wiki/Scraper-Reference) page).
2. Add the appropriate class attributes and filter options (see the [Scraper Reference](./scraper-reference.md) page).
3. Check that the scraper is listed in `thor docs:list`.
4. Create filters specific to the scraper in the `lib/docs/filters/[my_doc]/` directory and add them to the class's [filter stacks](https://github.com/Thibaut/devdocs/wiki/Scraper-Reference#filter-stacks). You may create any number of filters but will need at least the following two:
* A [`CleanHtml`](https://github.com/Thibaut/devdocs/wiki/Filter-Reference#cleanhtmlfilter) filter whose task is to clean the HTML markup (e.g. adding `id` attributes to headings) and remove everything superfluous and/or nonessential.
* An [`Entries`](https://github.com/Thibaut/devdocs/wiki/Filter-Reference#entriesfilter) filter whose task is to determine the pages' metadata (the list of entries, each with a name, type and path).
The [Filter Reference](https://github.com/Thibaut/devdocs/wiki/Filter-Reference) page has all the details about filters.
4. Create filters specific to the scraper in the `lib/docs/filters/[my_doc]/` directory and add them to the class's [filter stacks](./scraper-reference.md#filter-stacks). You may create any number of filters but will need at least the following two:
* A [`CleanHtml`](./filter-reference.md#cleanhtmlfilter) filter whose task is to clean the HTML markup (e.g. adding `id` attributes to headings) and remove everything superfluous and/or nonessential.
* An [`Entries`](./filter-reference.md#entriesfilter) filter whose task is to determine the pages' metadata (the list of entries, each with a name, type and path).
The [Filter Reference](./filter-reference.md) page has all the details about filters.
5. Using the `thor docs:page [my_doc] [path]` command, check that the scraper works properly. Files will appear in the `public/docs/[my_doc]/` directory (but not inside the app as the command doesn't touch the index). `path` in this case refers to either the remote path (if using `UrlScraper`) or the local path (if using `FileScraper`).
6. Generate the full documentation using the `thor docs:generate [my_doc] --force` command. Additionally, you can use the `--verbose` option to see which files are being created/updated/deleted (useful to see what changed since the last run), and the `--debug` option to see which URLs are being requested and added to the queue (useful to pin down which page adds unwanted URLs to the queue).
7. Start the server, open the app, enable the documentation, and see how everything plays out.
@ -19,4 +19,4 @@ Adding a documentation may look like a daunting task but once you get the hang o
If the documentation includes more than a few hundreds pages and is available for download, try to scrape it locally (e.g. using `FileScraper`). It'll make the development process much faster and avoids putting too much load on the source site. (It's not a problem if your scraper is coupled to your local setup, just explain how it works in your pull request.)
Finally, try to document your scraper and filters' behavior as much as possible using comments (e.g. why some URLs are ignored, HTML markup removed, metadata that way, etc.). It'll make updating the documentation much easier.
Finally, try to document your scraper and filters' behavior as much as possible using comments (e.g. why some URLs are ignored, HTML markup removed, metadata that way, etc.). It'll make updating the documentation much easier.