gitlab-markup 1.5.0.pre

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +5 -0
  3. data/.gitlab-ci.yml +27 -0
  4. data/.kick +26 -0
  5. data/.travis.yml +21 -0
  6. data/CONTRIBUTING.md +49 -0
  7. data/Gemfile +13 -0
  8. data/HISTORY.md +128 -0
  9. data/LICENSE +20 -0
  10. data/README.md +58 -0
  11. data/Rakefile +17 -0
  12. data/bin/github-markup +10 -0
  13. data/gitlab-markup.gemspec +25 -0
  14. data/lib/github-markup.rb +6 -0
  15. data/lib/github/commands/rest2html +200 -0
  16. data/lib/github/markup.rb +55 -0
  17. data/lib/github/markup/command_implementation.rb +71 -0
  18. data/lib/github/markup/gem_implementation.rb +30 -0
  19. data/lib/github/markup/implementation.rb +28 -0
  20. data/lib/github/markup/markdown.rb +60 -0
  21. data/lib/github/markup/rdoc.rb +26 -0
  22. data/lib/github/markups.rb +50 -0
  23. data/script/bootstrap +8 -0
  24. data/script/cibuild +20 -0
  25. data/test/fixtures/fail.sh +3 -0
  26. data/test/markup_test.rb +116 -0
  27. data/test/markups/README.asciidoc +23 -0
  28. data/test/markups/README.asciidoc.html +59 -0
  29. data/test/markups/README.creole +34 -0
  30. data/test/markups/README.creole.html +20 -0
  31. data/test/markups/README.litcoffee +59 -0
  32. data/test/markups/README.litcoffee.html +66 -0
  33. data/test/markups/README.markdown +2 -0
  34. data/test/markups/README.markdown.html +4 -0
  35. data/test/markups/README.mediawiki +30 -0
  36. data/test/markups/README.mediawiki.html +60 -0
  37. data/test/markups/README.noformat +2 -0
  38. data/test/markups/README.noformat.html +2 -0
  39. data/test/markups/README.org +131 -0
  40. data/test/markups/README.org.html +139 -0
  41. data/test/markups/README.pod +88 -0
  42. data/test/markups/README.pod.html +85 -0
  43. data/test/markups/README.rdoc +6 -0
  44. data/test/markups/README.rdoc.html +12 -0
  45. data/test/markups/README.rmd +3 -0
  46. data/test/markups/README.rmd.html +6 -0
  47. data/test/markups/README.rst +79 -0
  48. data/test/markups/README.rst.html +91 -0
  49. data/test/markups/README.rst.txt +21 -0
  50. data/test/markups/README.rst.txt.html +37 -0
  51. data/test/markups/README.textile +2 -0
  52. data/test/markups/README.textile.html +4 -0
  53. data/test/markups/README.toc.rst +30 -0
  54. data/test/markups/README.toc.rst.html +32 -0
  55. data/test/markups/README.txt +2 -0
  56. data/test/markups/README.txt.html +2 -0
  57. metadata +216 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5d773b18a0aefc150bf87e380d9efa3ed0c63425
4
+ data.tar.gz: 9021cc4c1aef932a5227a03e6ce7245f914d41fa
5
+ SHA512:
6
+ metadata.gz: 06675a6df4b9b39573c2b7b9f4178ed49fe5eec73c3185548c3a550aaed65f642c11466af9a9bdaea4d6f2f4463117b96d75140ad90f234d0009a339d6826d2f
7
+ data.tar.gz: af0a1dba6a7e9c4335f761af703f8a58d73bcc0d9d3a7f1f0d0bac2def20cbe68ee1e8b3a80e01baf7de90f28b7780dfabb7a0c733da199c79a18b9873bcea4b
@@ -0,0 +1,5 @@
1
+ *.pyc
2
+ pkg/
3
+ .bundle
4
+ Gemfile.lock
5
+ vendor/
@@ -0,0 +1,27 @@
1
+ variables:
2
+ LANG: "C.UTF-8"
3
+
4
+ .specs: &specs
5
+ cache:
6
+ paths:
7
+ - vendor/ruby
8
+ before_script:
9
+ - apt update
10
+ - apt install python-pip git build-essential -y
11
+ - pip install docutils
12
+ - bundle install --jobs $(nproc)
13
+ script:
14
+ - bundle exec rake test
15
+
16
+ ruby-20:
17
+ image: ruby:2.0
18
+ <<: *specs
19
+ ruby-21:
20
+ image: ruby:2.1
21
+ <<: *specs
22
+ ruby-22:
23
+ image: ruby:2.2
24
+ <<: *specs
25
+ ruby-23:
26
+ image: ruby:2.3
27
+ <<: *specs
data/.kick ADDED
@@ -0,0 +1,26 @@
1
+ # take control of the growl notifications
2
+ module GrowlHacks
3
+ def growl(type, subject, body, *args, &block)
4
+ case type
5
+ when Kicker::GROWL_NOTIFICATIONS[:succeeded]
6
+ puts subject = "Success"
7
+ body = body.split("\n").last
8
+ when Kicker::GROWL_NOTIFICATIONS[:failed]
9
+ subject = "Failure"
10
+ puts body
11
+ body = body.split("\n").last
12
+ else
13
+ return nil
14
+ end
15
+ super(type, subject, body, *args, &block)
16
+ end
17
+ end
18
+
19
+ Kicker.send :extend, GrowlHacks
20
+
21
+ # no logging
22
+ Kicker::Utils.module_eval do
23
+ def log(message)
24
+ nil
25
+ end
26
+ end
@@ -0,0 +1,21 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - 2.1.1
5
+ - jruby-19mode
6
+ jdk:
7
+ - oraclejdk8
8
+ notifications:
9
+ email: false
10
+ git:
11
+ depth: 10
12
+ before_install: sudo pip install docutils
13
+ cache:
14
+ - bundler
15
+ - pip
16
+ env:
17
+ global:
18
+ - "JRUBY_OPTS=-Xcext.enabled=true"
19
+ matrix:
20
+ allow_failures:
21
+ - rvm: jruby-19mode
@@ -0,0 +1,49 @@
1
+ # Contributing
2
+
3
+ This project adheres to the [Open Code of Conduct][code-of-conduct]. By participating, you are expected to honor this code.
4
+ [code-of-conduct]: http://todogroup.org/opencodeofconduct/#GitHub%20Markup/opensource@github.com
5
+
6
+ This library's only job is to decide which markup format to use and call out to an external library to convert the markup to HTML (see the [README](README.md) for more information on how markup is rendered on GitHub.com).
7
+
8
+ If you are having an issue with:
9
+
10
+ * **Syntax highlighting** - see [github/linguist](https://github.com/github/linguist/blob/master/CONTRIBUTING.md#fixing-syntax-highlighting)
11
+ * **Markdown on GitHub** - contact support@github.com
12
+ * **Styling issues on GitHub** - see [primer/markdown](https://github.com/primer/markdown)
13
+
14
+ Anything else - [search open issues](https://github.com/github/markup/issues) or create an issue and and we'll help point you in the right direction.
15
+
16
+ ## Submitting a Merge Request
17
+
18
+ 1. Fork it.
19
+ 2. Create a branch (`git checkout -b my_markup`)
20
+ 3. Commit your changes (`git commit -am "Added Snarkdown"`)
21
+ 4. Push to the branch (`git push origin my_markup`)
22
+ 5. Open a [Merge Request][1]
23
+ 6. Enjoy a refreshing Diet Coke and wait
24
+
25
+ ## Testing
26
+
27
+ To run the tests:
28
+
29
+ $ rake
30
+
31
+ If nothing complains, congratulations!
32
+
33
+ ## Releasing a new version
34
+
35
+ If you are the current maintainer of this gem:
36
+
37
+ 0. Bump the version number in `lib/github-markup.rb`, adhering to [Semantic Versioning](http://semver.org/)
38
+ 0. Update `HISTORY.md`
39
+ 0. Test the latest version on GitHub
40
+ 0. Build the new version with `rake build`
41
+ 0. Copy `pkg/gitlab-markup*.gem` to `vendor/cache` in your local checkout of GitHub
42
+ 0. Update the version for `gitlab-markup` in the `Gemfile`
43
+ 0. Run `bundle update --local gitlab-markup`
44
+ 0. Run any relevant tests and test it manually from the browser.
45
+ 0. Push the new gem release with `rake release`. If you don't have permission to release to rubygems.org, contact one of the existing owners (`gem owners github-markup`) and ask them to add you.
46
+
47
+ [1]: https://gitlab.com/gitlab-org/gitlab-markup/merge_requests
48
+ [r2h]: lib/github/commands/rest2html
49
+ [r2hc]: lib/github/markups.rb#L51
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "http://rubygems.org"
2
+ gemspec
3
+
4
+ gem "posix-spawn", :platforms => :ruby
5
+ gem "redcarpet", :platforms => :ruby
6
+ gem "kramdown", :platforms => :jruby
7
+ gem "RedCloth"
8
+ gem "rdoc", "~>3.6"
9
+ gem "org-ruby", "~> 0.9"
10
+ gem "creole", "~>0.5.0"
11
+ gem "wikicloth", "=0.8.1", :platforms => :ruby
12
+ gem "asciidoctor", "= 1.5.2"
13
+ gem "rake"
@@ -0,0 +1,128 @@
1
+ ## 1.5.0 (2016-10-13)
2
+
3
+ * GitLab CI support
4
+ * Fixed `.pod` test files to use newer syntax.
5
+ * Changes to support Python 3 (it's not out-of-the box yet, you still need to patch code)
6
+ * Gem published as 'gitlab-markup'
7
+
8
+ ## 1.3.3 (2015-02-17)
9
+
10
+ * Address a slight typo with `POSIX` [#456](https://github.com/github/markup/pull/456)
11
+
12
+ [Full changelog](https://github.com/github/markup/compare/v1.3.2...v1.3.3)
13
+
14
+ ## 1.3.2 (2015-02-17)
15
+
16
+ * RST: Output code instead of tt for inline literals [#370](https://github.com/github/markup/pull/370)
17
+ * RST: Add IDs to headers so that `.. contents` works with `.. sectnum` [#391](https://github.com/github/markup/pull/391)
18
+
19
+ [Full changelog](https://github.com/github/markup/compare/v1.3.1...v1.3.2)
20
+
21
+ ## 1.3.1 (2014-11-13)
22
+
23
+ * Fix name error when trying to use newer versions of RedCarpet [#387](https://github.com/github/markup/pull/387)
24
+
25
+ [Full changelog](https://github.com/github/markup/compare/v1.3.0...v1.3.1)
26
+
27
+ ## 1.3.0 (2014-09-11)
28
+
29
+ * Extend the field limit for tables to 50 characters for RST [#306](https://github.com/github/markup/pull/306)
30
+ * Add `.mkdn` as a supported markdown extension [#308](https://github.com/github/markup/pull/308)
31
+ * Upgrade wikicloth to 0.8.1 [#317](https://github.com/github/markup/pull/317)
32
+ * Force encoding of posix-spawn output [#338](https://github.com/github/markup/pull/338)
33
+ * Add `.rmd` as a supported markdown extension [#343](https://github.com/github/markup/pull/343)
34
+
35
+ [Full changelog](https://github.com/github/markup/compare/v1.2.1...v1.3.0)
36
+
37
+ ## 1.2.1 (2014-04-23)
38
+
39
+ * Disable RST warnings [#290](https://github.com/github/markup/pull/290)
40
+
41
+ [Full changelog](https://github.com/github/markup/compare/v1.2.0...v1.2.1)
42
+
43
+ ## 1.1.1 (2014-04-03)
44
+
45
+ * Upgrade to org-ruby 0.9.1
46
+ * Set default encoding to UTF-8 for Python 2
47
+
48
+ ## 1.1.0 (2014-03-10)
49
+
50
+ * Raise GitHub::Markup::CommandError if external command exits with a non-zero status.
51
+ * Remove support for literate Haskell (see #266)
52
+
53
+ ## 0.5.1 (2010-09-30)
54
+
55
+ * Support relative path links in rdoc
56
+
57
+ ## 0.5.0 (2010-07-07)
58
+
59
+ * Added creole support
60
+
61
+ ## 0.4.0 (2010-04-23)
62
+
63
+ * Removed man page support until it's ready.
64
+
65
+ ## 0.3.3 (2010-03-29)
66
+
67
+ * UTF-8 works with ReST now.
68
+
69
+ ## 0.3.2 (2010-03-25)
70
+
71
+ * Improved test runner
72
+ * Forgive ReST problems that aren't user errors.
73
+
74
+ ## 0.3.1 (2010-03-22)
75
+
76
+ * Add .rst.txt extension
77
+ * Fix ASCII encoding error while using print u'\u010c' non-ASCII char and similar.
78
+
79
+ ## 0.3.0 (2010-03-11)
80
+
81
+ * man rendering
82
+ * `github-markup` command line runner
83
+
84
+ ## 0.2.2 (2010-02-09)
85
+
86
+ * pod fixes from Ricardo Signes
87
+
88
+ ## 0.2.1 (2010-01-25)
89
+
90
+ * ReST fixes from Michael Jones
91
+
92
+ ## 0.2.0 (2010-01-10)
93
+
94
+ * org-mode support
95
+
96
+ ## 0.1.7 (2009-11-17)
97
+
98
+ * Ditch asciidoc2html, call asciidoc directly
99
+
100
+ ## 0.1.6 (2009-11-17)
101
+
102
+ * mdown
103
+
104
+ ## 0.1.5 (2009-11-17)
105
+
106
+ * Actually, if we can't render a thing then don't. Not once, not never.
107
+
108
+ ## 0.1.4 (2009-11-17)
109
+
110
+ * Bugfix: Missing commands return the input (instead of nothing)
111
+
112
+ ## 0.1.3 (2009-11-02)
113
+
114
+ * Strip the INDEX comments from POD
115
+
116
+ ## 0.1.2 (2009-11-02)
117
+
118
+ * Renamed to `github-markup`
119
+ * Bugfix: POD rendering works now, not just index
120
+
121
+ ## 0.1.1 (2009-11-02)
122
+
123
+ * Added `GitHub::Markup.can_render?` helper.
124
+ * Bugfix: Actually check file extensions
125
+
126
+ ## 0.1.0 (2009-11-02)
127
+
128
+ * First release
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 GitHub
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,58 @@
1
+ GitHub Markup
2
+ =============
3
+
4
+ [![build status](https://gitlab.com/gitlab-org/gitlab-markup/badges/master/build.svg)](https://gitlab.com/gitlab-org/gitlab-markup/commits/master)
5
+
6
+ This library is the first step of a journey that every markup file in a repository goes on before it is rendered on GitHub.com:
7
+
8
+ 0. This library converts the raw markup to HTML. See the list of [supported markup formats](#markups) below.
9
+ 0. The HTML is sanitized, aggressively removing things that could harm you and your kin—such as `script` tags, inline-styles, and `class` or `id` attributes. See the [sanitization filter](https://github.com/jch/html-pipeline/blob/master/lib/html/pipeline/sanitization_filter.rb) for the full whitelist.
10
+ 0. Syntax highlighting is performed on code blocks. See [github/linguist](https://github.com/github/linguist#syntax-highlighting) for more information about syntax highlighting.
11
+ 0. The HTML is passed through other filters in the [html-pipeline](https://github.com/jch/html-pipeline) that add special sauce, such as [emoji](https://github.com/jch/html-pipeline/blob/master/lib/html/pipeline/emoji_filter.rb), [task lists](https://github.com/github/task_list/blob/master/lib/task_list/filter.rb), [named anchors](https://github.com/jch/html-pipeline/blob/master/lib/html/pipeline/toc_filter.rb), [CDN caching for images](https://github.com/jch/html-pipeline/blob/master/lib/html/pipeline/camo_filter.rb), and [autolinking](https://github.com/jch/html-pipeline/blob/master/lib/html/pipeline/autolink_filter.rb).
12
+ 0. The resulting HTML is rendered on GitHub.com.
13
+
14
+ Please see our [contributing guidelines](CONTRIBUTING.md) before reporting an issue.
15
+
16
+ Markups
17
+ -------
18
+
19
+ The following markups are supported. The dependencies listed are required if
20
+ you wish to run the library. You can also run `script/bootstrap` to fetch them all.
21
+
22
+ * [.markdown, .mdown, .mkdn, .md](http://daringfireball.net/projects/markdown/) -- `gem install redcarpet` (https://github.com/vmg/redcarpet)
23
+ * [.textile](http://www.textism.com/tools/textile/) -- `gem install RedCloth`
24
+ * [.rdoc](http://rdoc.sourceforge.net/) -- `gem install rdoc -v 3.6.1`
25
+ * [.org](http://orgmode.org/) -- `gem install org-ruby`
26
+ * [.creole](http://wikicreole.org/) -- `gem install creole`
27
+ * [.mediawiki, .wiki](http://www.mediawiki.org/wiki/Help:Formatting) -- `gem install wikicloth`
28
+ * [.rst](http://docutils.sourceforge.net/rst.html) -- `easy_install docutils`
29
+ * [.asciidoc, .adoc, .asc](http://asciidoc.org/) -- `gem install asciidoctor` (http://asciidoctor.org)
30
+ * [.pod](http://search.cpan.org/dist/perl/pod/perlpod.pod) -- `Pod::Simple::HTML`
31
+ comes with Perl >= 5.10. Lower versions should install [Pod::Simple](http://search.cpan.org/~dwheeler/Pod-Simple-3.28/lib/Pod/Simple.pod) from CPAN.
32
+
33
+ Installation
34
+ -----------
35
+
36
+ ```
37
+ gem install github-markup
38
+ ```
39
+
40
+ Usage
41
+ -----
42
+
43
+ ```ruby
44
+ require 'github/markup'
45
+ GitHub::Markup.render('README.markdown', "* One\n* Two")
46
+ ```
47
+
48
+ Or, more realistically:
49
+
50
+ ```ruby
51
+ require 'github/markup'
52
+ GitHub::Markup.render(file, File.read(file))
53
+ ```
54
+
55
+ Contributing
56
+ ------------
57
+
58
+ See [Contributing](CONTRIBUTING.md)
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env rake
2
+
3
+ require "bundler/gem_tasks"
4
+
5
+ require 'rake/testtask'
6
+ Rake::TestTask.new(:test) do |test|
7
+ test.libs << 'lib' << 'test'
8
+ test.pattern = 'test/**/*_test.rb'
9
+ test.verbose = true
10
+ end
11
+
12
+ desc "Open an irb session preloaded with this library"
13
+ task :console do
14
+ sh "irb -I lib -r bundler/setup -r github/markup"
15
+ end
16
+
17
+ task :default => :test
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.unshift File.dirname(__FILE__) + "/../lib"
4
+ require 'github/markup'
5
+
6
+ if ARGV[0] && File.exists?(file = ARGV[0])
7
+ puts GitHub::Markup.render(file)
8
+ else
9
+ puts "usage: #$0 FILE"
10
+ end
@@ -0,0 +1,25 @@
1
+ require File.expand_path("../lib/github-markup", __FILE__)
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "gitlab-markup"
5
+ s.version = GitHub::Markup::VERSION
6
+ s.summary = "The code GitHub uses to render README.markup"
7
+ s.description = "This gem is used by GitHub to render any fancy markup such " +
8
+ "as Markdown, Textile, Org-Mode, etc. Fork it and add your own!"
9
+ s.authors = ["Chris Wanstrath"]
10
+ s.email = "chris@ozmm.org"
11
+ s.homepage = "https://gitlab.com/gitlab-org/gitlab-markup"
12
+ s.license = "MIT"
13
+
14
+ s.files = `git ls-files`.split($\)
15
+ s.executables = s.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
16
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
17
+ s.require_paths = %w[lib]
18
+
19
+ s.add_development_dependency 'activesupport', '~> 4.0'
20
+ s.add_development_dependency 'minitest', '~> 5.4.3'
21
+ s.add_development_dependency 'html-pipeline', '~> 1.0'
22
+ s.add_development_dependency 'sanitize', '~> 2.1.0'
23
+ s.add_development_dependency 'nokogiri', '~> 1.6.1'
24
+ s.add_development_dependency 'nokogiri-diff', '~> 0.2.0'
25
+ end
@@ -0,0 +1,6 @@
1
+ module GitHub
2
+ module Markup
3
+ VERSION = '1.5.0.pre'
4
+ Version = VERSION
5
+ end
6
+ end
@@ -0,0 +1,200 @@
1
+ #!/usr/bin/env python
2
+ """
3
+ rest2html - A small wrapper file for parsing ReST files at GitHub.
4
+
5
+ Written in 2008 by Jannis Leidel <jannis@leidel.info>
6
+
7
+ Brandon Keepers <bkeepers@github.com>
8
+ Bryan Veloso <bryan@revyver.com>
9
+ Chris Wanstrath <chris@ozmm.org>
10
+ Dave Abrahams <dave@boostpro.com>
11
+ Garen Torikian <garen@github.com>
12
+ Gasper Zejn <zejn@kiberpipa.org>
13
+ Michael Jones <m.pricejones@gmail.com>
14
+ Sam Whited <sam@samwhited.com>
15
+ Tyler Chung <zonyitoo@gmail.com>
16
+ Vicent Marti <tanoku@gmail.com>
17
+
18
+ To the extent possible under law, the author(s) have dedicated all copyright
19
+ and related and neighboring rights to this software to the public domain
20
+ worldwide. This software is distributed without any warranty.
21
+
22
+ You should have received a copy of the CC0 Public Domain Dedication along with
23
+ this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
24
+ """
25
+
26
+ __author__ = "Jannis Leidel"
27
+ __license__ = "CC0"
28
+ __version__ = "0.1"
29
+
30
+ import sys
31
+ import os
32
+
33
+ # This fixes docutils failing with unicode parameters to CSV-Table. The -S
34
+ # switch and the following 3 lines can be removed after upgrading to python 3.
35
+ if sys.version_info[0] < 3:
36
+ reload(sys)
37
+ sys.setdefaultencoding('utf-8')
38
+
39
+ import site
40
+
41
+ try:
42
+ import locale
43
+ locale.setlocale(locale.LC_ALL, '')
44
+ except:
45
+ pass
46
+
47
+ import codecs
48
+ import io
49
+
50
+ from docutils import nodes
51
+ from docutils.parsers.rst import directives, roles
52
+ from docutils.parsers.rst.directives.body import CodeBlock
53
+ from docutils.core import publish_parts
54
+ from docutils.writers.html4css1 import Writer, HTMLTranslator
55
+
56
+ SETTINGS = {
57
+ 'cloak_email_addresses': False,
58
+ 'file_insertion_enabled': False,
59
+ 'raw_enabled': True,
60
+ 'strip_comments': True,
61
+ 'doctitle_xform': True,
62
+ 'sectsubtitle_xform': True,
63
+ 'initial_header_level': 2,
64
+ 'report_level': 5,
65
+ 'syntax_highlight': 'none',
66
+ 'math_output': 'latex',
67
+ 'field_name_limit': 50,
68
+ }
69
+
70
+
71
+ class DoctestDirective(CodeBlock):
72
+ """Render Sphinx 'doctest:: [group]' blocks as 'code:: python'
73
+ """
74
+
75
+ def run(self):
76
+ """Discard any doctest group argument, render contents as python code
77
+ """
78
+ self.arguments = ['python']
79
+ return super(DoctestDirective, self).run()
80
+
81
+
82
+ class GitHubHTMLTranslator(HTMLTranslator):
83
+
84
+ # removes the <div class="document"> tag wrapped around docs
85
+ # see also: http://bit.ly/1exfq2h (warning! sourceforge link.)
86
+ def depart_document(self, node):
87
+ HTMLTranslator.depart_document(self, node)
88
+ self.html_body.pop(0) # pop the starting <div> off
89
+ self.html_body.pop() # pop the ending </div> off
90
+
91
+ # technique for visiting sections, without generating additional divs
92
+ # see also: http://bit.ly/NHtyRx
93
+ # the a is to support ::contents with ::sectnums: http://git.io/N1yC
94
+ def visit_section(self, node):
95
+ id_attribute = node.attributes['ids'][0]
96
+ self.body.append('<a name="%s"></a>\n' % id_attribute)
97
+ self.section_level += 1
98
+
99
+ def depart_section(self, node):
100
+ self.section_level -= 1
101
+
102
+ def visit_literal_block(self, node):
103
+ classes = node.attributes['classes']
104
+ if len(classes) >= 2 and classes[0] == 'code':
105
+ language = classes[1]
106
+ del classes[:]
107
+ self.body.append(self.starttag(node, 'pre', lang=language))
108
+ else:
109
+ self.body.append(self.starttag(node, 'pre'))
110
+
111
+ # always wrap two-backtick rst inline literals in <code>, not <tt>
112
+ # this also avoids the generation of superfluous <span> tags
113
+ def visit_literal(self, node):
114
+ self.body.append(self.starttag(node, 'code', suffix=''))
115
+
116
+ def depart_literal(self, node):
117
+ self.body.append('</code>')
118
+
119
+ def visit_table(self, node):
120
+ classes = ' '.join(['docutils', self.settings.table_style]).strip()
121
+ self.body.append(
122
+ self.starttag(node, 'table', CLASS=classes))
123
+
124
+ def depart_table(self, node):
125
+ self.body.append('</table>\n')
126
+
127
+ def depart_image(self, node):
128
+ uri = node['uri']
129
+ ext = os.path.splitext(uri)[1].lower()
130
+ # we need to swap RST's use of `object` with `img` tags
131
+ # see http://git.io/5me3dA
132
+ if ext == ".svg":
133
+ # preserve essential attributes
134
+ atts = {}
135
+ for attribute, value in node.attributes.items():
136
+ # we have no time for empty values
137
+ if value:
138
+ if attribute == "uri":
139
+ atts['src'] = value
140
+ else:
141
+ atts[attribute] = value
142
+
143
+ # toss off `object` tag
144
+ self.body.pop()
145
+ # add on `img` with attributes
146
+ self.body.append(self.starttag(node, 'img', **atts))
147
+ self.body.append(self.context.pop())
148
+
149
+
150
+ def kbd(name, rawtext, text, lineno, inliner, options=None, content=None):
151
+
152
+ return [nodes.raw('', '<kbd>%s</kbd>' % text, format='html')], []
153
+
154
+
155
+ def main():
156
+ """
157
+ Parses the given ReST file or the redirected string input and returns the
158
+ HTML body.
159
+
160
+ Usage: rest2html < README.rst
161
+ rest2html README.rst
162
+ """
163
+ try:
164
+ text = codecs.open(sys.argv[1], 'r', 'utf-8').read()
165
+ except IOError: # given filename could not be found
166
+ return ''
167
+ except IndexError: # no filename given
168
+ if sys.version_info[0] < 3: # python 2.x
169
+ text = sys.stdin.read()
170
+ else: # python 3
171
+ input_stream = io.TextIOWrapper(sys.stdin.buffer, encoding='utf-8')
172
+ text = input_stream.read()
173
+
174
+ writer = Writer()
175
+ writer.translator_class = GitHubHTMLTranslator
176
+
177
+ roles.register_canonical_role('kbd', kbd)
178
+
179
+ # Render source code in Sphinx doctest blocks
180
+ directives.register_directive('doctest', DoctestDirective)
181
+
182
+ parts = publish_parts(text, writer=writer, settings_overrides=SETTINGS)
183
+ if 'html_body' in parts:
184
+ html = parts['html_body']
185
+
186
+ # publish_parts() in python 2.x return dict values as Unicode type
187
+ # in py3k Unicode is unavailable and values are of str type
188
+ if isinstance(html, str):
189
+ return html
190
+ else:
191
+ return html.encode('utf-8')
192
+ return ''
193
+
194
+ if __name__ == '__main__':
195
+ if sys.version_info[0] < 3: # python 2.x
196
+ sys.stdout.write("%s%s" % (main(), "\n"))
197
+ else: # python 3
198
+ output_stream = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
199
+ output_stream.write("%s%s" % (main(), "\n"))
200
+ sys.stdout.flush()