Commit 2000fb00 authored by Lin Jen-Shin's avatar Lin Jen-Shin

Merge branch 'asciidoc-attr' into 'master'

Set AsciiDoc attributes related to the requested path

See merge request gitlab-org/gitlab!22313
parents 285b752f e59de28e
---
title: Support Asciidoc docname attribute
merge_request: 22313
author: Jouke Witteveen
type: added
...@@ -91,6 +91,8 @@ stem:[sqrt(4) = 2] ...@@ -91,6 +91,8 @@ stem:[sqrt(4) = 2]
### Attributes ### Attributes
**User-defined attributes**
```asciidoc ```asciidoc
// define attributes in the document header // define attributes in the document header
:name: value :name: value
...@@ -104,6 +106,15 @@ C{pp} is not required, only Ruby. ...@@ -104,6 +106,15 @@ C{pp} is not required, only Ruby.
Use a leading backslash to output a word enclosed in curly braces, like \{name}. Use a leading backslash to output a word enclosed in curly braces, like \{name}.
``` ```
**Environment attributes**
GitLab sets the following environment attributes:
| Attribute | Description |
| :-------------- | :--------------------------------------------------------------------------------------------------------------------- |
| `docname` | Root name of the source document (no leading path or file extension). |
| `outfilesuffix` | File extension corresponding to the backend output (defaults to `.adoc` to make inter-document cross references work). |
### Links ### Links
```asciidoc ```asciidoc
......
...@@ -25,6 +25,19 @@ module Gitlab ...@@ -25,6 +25,19 @@ module Gitlab
'max-include-depth' => MAX_INCLUDE_DEPTH 'max-include-depth' => MAX_INCLUDE_DEPTH
}.freeze }.freeze
def self.path_attrs(path)
return {} unless path
{
# Set an empty docname if the path is a directory
'docname' => if path[-1] == ::File::SEPARATOR
''
else
::File.basename(path, '.*')
end
}
end
# Public: Converts the provided Asciidoc markup into HTML. # Public: Converts the provided Asciidoc markup into HTML.
# #
# input - the source text in Asciidoc format # input - the source text in Asciidoc format
...@@ -35,9 +48,10 @@ module Gitlab ...@@ -35,9 +48,10 @@ module Gitlab
include_processor ::Gitlab::Asciidoc::IncludeProcessor.new(context) include_processor ::Gitlab::Asciidoc::IncludeProcessor.new(context)
end end
extra_attrs = path_attrs(context[:requested_path])
asciidoc_opts = { safe: :secure, asciidoc_opts = { safe: :secure,
backend: :gitlab_html5, backend: :gitlab_html5,
attributes: DEFAULT_ADOC_ATTRS, attributes: DEFAULT_ADOC_ATTRS.merge(extra_attrs),
extensions: extensions } extensions: extensions }
context[:pipeline] = :ascii_doc context[:pipeline] = :ascii_doc
......
...@@ -46,6 +46,27 @@ module Gitlab ...@@ -46,6 +46,27 @@ module Gitlab
end end
end end
context "with requested path" do
input = <<~ADOC
Document name: {docname}.
ADOC
it "ignores {docname} when not available" do
expect(render(input, {})).to include(input.strip)
end
[
['/', '', 'root'],
['README', 'README', 'just a filename'],
['doc/api/', '', 'a directory'],
['doc/api/README.adoc', 'README', 'a complete path']
].each do |path, basename, desc|
it "sets {docname} for #{desc}" do
expect(render(input, { requested_path: path })).to include(": #{basename}.")
end
end
end
context "XSS" do context "XSS" do
items = { items = {
'link with extra attribute' => { 'link with extra attribute' => {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment