mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Add filename option to code-block
Prepend the given filename to the code block
This commit is contained in:
parent
445bb5daec
commit
05ba0766b3
@ -57,6 +57,7 @@ class CodeBlock(Directive):
|
|||||||
option_spec = {
|
option_spec = {
|
||||||
'linenos': directives.flag,
|
'linenos': directives.flag,
|
||||||
'emphasize-lines': directives.unchanged_required,
|
'emphasize-lines': directives.unchanged_required,
|
||||||
|
'filename': directives.unchanged_required,
|
||||||
}
|
}
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
@ -75,6 +76,9 @@ class CodeBlock(Directive):
|
|||||||
|
|
||||||
literal = nodes.literal_block(code, code)
|
literal = nodes.literal_block(code, code)
|
||||||
literal['language'] = self.arguments[0]
|
literal['language'] = self.arguments[0]
|
||||||
|
filename = self.options.get('filename')
|
||||||
|
if filename:
|
||||||
|
literal['filename'] = filename
|
||||||
literal['linenos'] = 'linenos' in self.options
|
literal['linenos'] = 'linenos' in self.options
|
||||||
if hl_lines is not None:
|
if hl_lines is not None:
|
||||||
literal['highlight_args'] = {'hl_lines': hl_lines}
|
literal['highlight_args'] = {'hl_lines': hl_lines}
|
||||||
|
@ -462,3 +462,17 @@ div.viewcode-block:target {
|
|||||||
border-top: 1px solid #ac9;
|
border-top: 1px solid #ac9;
|
||||||
border-bottom: 1px solid #ac9;
|
border-bottom: 1px solid #ac9;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
div.code-block-filename {
|
||||||
|
background-color: #ddd;
|
||||||
|
color: #333;
|
||||||
|
padding: 2px 5px;
|
||||||
|
font-family: monospace;
|
||||||
|
font-size: small;
|
||||||
|
}
|
||||||
|
|
||||||
|
div.code-block-filename + pre,
|
||||||
|
div.code-block-filename + div.highlight,
|
||||||
|
div.code-block-filename + div.highlight > pre {
|
||||||
|
margin-top: 0;
|
||||||
|
}
|
||||||
|
@ -308,3 +308,16 @@ div.viewcode-block:target {
|
|||||||
border-top: 1px solid #ac9;
|
border-top: 1px solid #ac9;
|
||||||
border-bottom: 1px solid #ac9;
|
border-bottom: 1px solid #ac9;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
div.code-block-filename {
|
||||||
|
background-color: #1c4e63;
|
||||||
|
color: #efefef;
|
||||||
|
padding: 2px 5px;
|
||||||
|
font-family: monospace;
|
||||||
|
font-size: small;
|
||||||
|
}
|
||||||
|
|
||||||
|
div.code-block-filename + pre,
|
||||||
|
div.code-block-filename + div.highlight > pre {
|
||||||
|
margin-top: 0;
|
||||||
|
}
|
||||||
|
@ -369,3 +369,7 @@ div.viewcode-block:target {
|
|||||||
margin: -1px -10px;
|
margin: -1px -10px;
|
||||||
padding: 0 12px;
|
padding: 0 12px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
div.code-block-filename {
|
||||||
|
font-family: monospace;
|
||||||
|
}
|
||||||
|
@ -243,3 +243,17 @@ div.viewcode-block:target {
|
|||||||
border-top: 1px solid #ac9;
|
border-top: 1px solid #ac9;
|
||||||
border-bottom: 1px solid #ac9;
|
border-bottom: 1px solid #ac9;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
div.code-block-filename {
|
||||||
|
background-color: #ddd;
|
||||||
|
color: #222;
|
||||||
|
border: 1px solid #C6C9CB;
|
||||||
|
padding: 2px 5px;
|
||||||
|
font-family: monospace;
|
||||||
|
font-size: small;
|
||||||
|
}
|
||||||
|
|
||||||
|
div.code-block-filename + pre,
|
||||||
|
div.code-block-filename + div.highlight > pre {
|
||||||
|
margin-top: 0;
|
||||||
|
}
|
||||||
|
@ -340,3 +340,16 @@ tt.xref {
|
|||||||
font-weight: normal;
|
font-weight: normal;
|
||||||
font-style: normal;
|
font-style: normal;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
div.code-block-filename {
|
||||||
|
background-color: #ddd;
|
||||||
|
color: #222;
|
||||||
|
padding: 2px 5px;
|
||||||
|
font-family: monospace;
|
||||||
|
font-size: small;
|
||||||
|
}
|
||||||
|
|
||||||
|
div.code-block-filename + pre,
|
||||||
|
div.code-block-filename + div.highlight > pre {
|
||||||
|
margin-top: 0;
|
||||||
|
}
|
||||||
|
@ -337,3 +337,17 @@ div.viewcode-block:target {
|
|||||||
border-top: 1px solid #ac9;
|
border-top: 1px solid #ac9;
|
||||||
border-bottom: 1px solid #ac9;
|
border-bottom: 1px solid #ac9;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
div.code-block-filename {
|
||||||
|
background-color: #ddd;
|
||||||
|
color: #222;
|
||||||
|
border: 1px solid #ccc;
|
||||||
|
padding: 2px 5px;
|
||||||
|
font-family: monospace;
|
||||||
|
font-size: small;
|
||||||
|
}
|
||||||
|
|
||||||
|
div.code-block-filename + pre,
|
||||||
|
div.code-block-filename + div.highlight > pre {
|
||||||
|
margin-top: 0;
|
||||||
|
}
|
||||||
|
@ -702,3 +702,15 @@ div.viewcode-block:target {
|
|||||||
margin: -1px -10px;
|
margin: -1px -10px;
|
||||||
padding: 0 10px;
|
padding: 0 10px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
div.code-block-filename {
|
||||||
|
background-color: #cceeff;
|
||||||
|
padding: 2px 5px;
|
||||||
|
font-family: monospace;
|
||||||
|
font-size: small;
|
||||||
|
}
|
||||||
|
|
||||||
|
div.code-block-filename + pre,
|
||||||
|
div.code-block-filename + div.highlight > pre {
|
||||||
|
margin-top: 0;
|
||||||
|
}
|
||||||
|
@ -256,6 +256,9 @@ class HTMLTranslator(BaseTranslator):
|
|||||||
**highlight_args)
|
**highlight_args)
|
||||||
starttag = self.starttag(node, 'div', suffix='',
|
starttag = self.starttag(node, 'div', suffix='',
|
||||||
CLASS='highlight-%s' % lang)
|
CLASS='highlight-%s' % lang)
|
||||||
|
if node.has_key('filename'):
|
||||||
|
starttag += '<div class="code-block-filename">%s</div>' % (
|
||||||
|
node['filename'],)
|
||||||
self.body.append(starttag + highlighted + '</div>\n')
|
self.body.append(starttag + highlighted + '</div>\n')
|
||||||
raise nodes.SkipNode
|
raise nodes.SkipNode
|
||||||
|
|
||||||
|
@ -1333,6 +1333,11 @@ class LaTeXTranslator(nodes.NodeVisitor):
|
|||||||
highlight_args['force'] = True
|
highlight_args['force'] = True
|
||||||
if 'linenos' in node:
|
if 'linenos' in node:
|
||||||
linenos = node['linenos']
|
linenos = node['linenos']
|
||||||
|
filename = node.get('filename')
|
||||||
|
if filename:
|
||||||
|
self.body.append('\n{\\colorbox[rgb]{0.9,0.9,0.9}'
|
||||||
|
'{\\makebox[\\textwidth][l]'
|
||||||
|
'{\\small\\texttt{%s}}}}\n' % (filename,))
|
||||||
def warner(msg):
|
def warner(msg):
|
||||||
self.builder.warn(msg, (self.curfilestack[-1], node.line))
|
self.builder.warn(msg, (self.curfilestack[-1], node.line))
|
||||||
hlcode = self.highlighter.highlight_block(code, lang, warn=warner,
|
hlcode = self.highlighter.highlight_block(code, lang, warn=warner,
|
||||||
|
Loading…
Reference in New Issue
Block a user