Skip to content

Commit ae91f4a

Browse files
committed
Add source-host support for GitLab and Bitbucket
GitHub as default, back-compatible. Use extensions for assets to avoid duplication and shipping unneeded files.
1 parent 0644af3 commit ae91f4a

26 files changed

Lines changed: 289 additions & 97 deletions

CHANGELOG.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,18 @@
99
* Support DocC-style autolinks and callouts in markdown.
1010
[John Fairhurst](https://github.com/johnfairh)
1111

12+
* Add `--source-host` option to support projects hosted on GitLab and
13+
Bitbucket as well as GitHub. Options `--source-host-url` and
14+
`--source-host-files-url` and new Mustache tags replace the 'github' versions
15+
which remain as back-compatibility aliases.
16+
[John Fairhurst](https://github.com/johnfairh)
17+
[#314](https://github.com/realm/jazzy/issues/314)
18+
1219
##### Bug Fixes
1320

14-
* None.
21+
* Fix source-host line number references in Swift symbolgraph mode, and
22+
in ObjC mode for references to one-line declarations.
23+
[John Fairhurst](https://github.com/johnfairh)
1524

1625
## 0.13.7
1726

README.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,9 @@ jazzy \
117117
--clean \
118118
--author Realm \
119119
--author_url https://realm.io \
120-
--github_url https://github.com/realm/realm-cocoa \
121-
--github-file-prefix https://github.com/realm/realm-cocoa/tree/v0.96.2 \
120+
--source-host github \
121+
--source-host-url https://github.com/realm/realm-cocoa \
122+
--source-host-files-url https://github.com/realm/realm-cocoa/tree/v0.96.2 \
122123
--module-version 0.96.2 \
123124
--build-tool-arguments -scheme,RealmSwift \
124125
--module RealmSwift \
@@ -159,8 +160,9 @@ jazzy \
159160
--clean \
160161
--author Realm \
161162
--author_url https://realm.io \
162-
--github_url https://github.com/realm/realm-cocoa \
163-
--github-file-prefix https://github.com/realm/realm-cocoa/tree/v2.2.0 \
163+
--source-host github \
164+
--source-host-url https://github.com/realm/realm-cocoa \
165+
--source-host-files-url https://github.com/realm/realm-cocoa/tree/v2.2.0 \
164166
--module-version 2.2.0 \
165167
--build-tool-arguments --objc,Realm/Realm.h,--,-x,objective-c,-isysroot,$(xcrun --show-sdk-path),-I,$(pwd) \
166168
--module Realm \
@@ -176,8 +178,9 @@ jazzy \
176178
--objc \
177179
--author AFNetworking \
178180
--author_url http://afnetworking.com \
179-
--github_url https://github.com/AFNetworking/AFNetworking \
180-
--github-file-prefix https://github.com/AFNetworking/AFNetworking/tree/2.6.2 \
181+
--source-host github \
182+
--source-host-url https://github.com/AFNetworking/AFNetworking \
183+
--source-host-files-url https://github.com/AFNetworking/AFNetworking/tree/2.6.2 \
181184
--module-version 2.6.2 \
182185
--umbrella-header AFNetworking/AFNetworking.h \
183186
--framework-root . \

lib/jazzy/config.rb

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -325,16 +325,42 @@ def hide_objc?
325325
'e.g. https://realm.io/docsets/realm.xml)',
326326
parse: ->(d) { URI(d) }
327327

328-
config_attr :github_url,
329-
command_line: ['-g', '--github_url URL'],
330-
description: 'GitHub URL of this project (e.g. '\
331-
'https://github.com/realm/realm-cocoa)',
328+
SOURCE_HOSTS = %w[github gitlab bitbucket].freeze
329+
330+
config_attr :source_host,
331+
command_line: "--source-host #{SOURCE_HOSTS.join(' | ')}",
332+
description: ['The source-code hosting site to be linked from documentation.',
333+
'This setting affects the logo image and link format.',
334+
"Default: 'github'"],
335+
default: 'github',
336+
parse: ->(host) do
337+
return host.to_sym if SOURCE_HOSTS.include?(host)
338+
339+
raise "Unsupported source_host '#{host}', "\
340+
"supported values: #{SOURCE_HOSTS.join(', ')}"
341+
end
342+
343+
config_attr :source_host_url,
344+
command_line: ['--source-host-url URL'],
345+
description: ["URL to link from the source host's logo.",
346+
'For example https://github.com/realm/realm-cocoa'],
332347
parse: ->(g) { URI(g) }
333348

334-
config_attr :github_file_prefix,
349+
alias_config_attr :github_url, :source_host_url,
350+
command_line: ['-g', '--github_url URL'],
351+
description: 'Back-compatibility alias for source_host_url.'
352+
353+
config_attr :source_host_files_url,
354+
command_line: '--source-host-files-url PREFIX',
355+
description: [
356+
"The base URL on the source host of the project's files, to link "\
357+
'from individual declarations.',
358+
'For example https://github.com/realm/realm-cocoa/tree/v0.87.1',
359+
]
360+
361+
alias_config_attr :github_file_prefix, :source_host_files_url,
335362
command_line: '--github-file-prefix PREFIX',
336-
description: 'GitHub URL file prefix of this project (e.g. '\
337-
'https://github.com/realm/realm-cocoa/tree/v0.87.1)'
363+
description: 'Back-compatibility alias for source_host_files_url'
338364

339365
config_attr :docset_playground_url,
340366
command_line: '--docset-playground-url URL',
@@ -476,9 +502,15 @@ def self.parse!
476502
)
477503
end
478504

505+
config.validate
506+
479507
config
480508
end
481509

510+
def warning(message)
511+
warn "WARNING: #{message}"
512+
end
513+
482514
# rubocop:disable Metrics/MethodLength
483515
def parse_command_line
484516
OptionParser.new do |opt|
@@ -510,6 +542,10 @@ def parse_command_line
510542
exit
511543
end
512544
end.parse!
545+
546+
unless ARGV.empty?
547+
warning "Leftover unused command-line text: #{ARGV}"
548+
end
513549
end
514550

515551
def parse_config_file
@@ -527,12 +563,12 @@ def parse_config_file
527563

528564
config_file.each do |key, value|
529565
unless attr = attrs_by_conf_key[key]
530-
message = "WARNING: Unknown config file attribute #{key.inspect}"
566+
message = "Unknown config file attribute #{key.inspect}"
531567
if matching_name = attrs_by_name[key]
532568
message +=
533569
" (Did you mean #{matching_name.first.config_file_key.inspect}?)"
534570
end
535-
warn message
571+
warning message
536572
next
537573
end
538574

@@ -541,6 +577,16 @@ def parse_config_file
541577

542578
self.base_path = nil
543579
end
580+
581+
def validate
582+
if source_host_configured &&
583+
source_host_url.nil? &&
584+
source_host_files_url.nil?
585+
warning 'Option `source_host` is set but has no effect without either '\
586+
'`source_host_url` or `source_host_files_url`.'
587+
end
588+
end
589+
544590
# rubocop:enable Metrics/MethodLength
545591

546592
def locate_config_file

lib/jazzy/doc_builder.rb

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def self.build_site(docs, coverage, options)
134134
SearchBuilder.build(source_module, output_dir)
135135
end
136136

137-
copy_extensions(output_dir)
137+
copy_extensions(source_module, output_dir)
138138
copy_theme_assets(output_dir)
139139

140140
DocsetBuilder.new(output_dir, source_module).build!
@@ -219,7 +219,10 @@ def self.copy_theme_assets(destination)
219219
end
220220
end
221221

222-
def self.copy_extensions(destination)
222+
def self.copy_extensions(source_module, destination)
223+
if source_host = source_module.host&.extension
224+
copy_extension(source_host, destination)
225+
end
223226
copy_extension('katex', destination) if Markdown.has_math
224227
end
225228

@@ -251,7 +254,14 @@ def self.document_markdown(source_module, doc_model, path_to_root)
251254
doc[:structure] = source_module.doc_structure
252255
doc[:module_name] = source_module.name
253256
doc[:author_name] = source_module.author_name
254-
doc[:github_url] = source_module.github_url
257+
if source_host = source_module.host
258+
doc[:source_host_name] = source_host.name
259+
doc[:source_host_url] = source_host.url
260+
doc[:source_host_image] = source_host.image
261+
doc[:source_host_item_url] = source_host.item_url(doc_model)
262+
doc[:github_url] = doc[:source_host_url]
263+
doc[:github_token_url] = doc[:source_host_item_url]
264+
end
255265
doc[:dash_url] = source_module.dash_url
256266
doc[:path_to_root] = path_to_root
257267
doc[:hide_name] = true
@@ -326,39 +336,14 @@ def self.generate_badge(coverage, options)
326336
end
327337
# rubocop:enable Metrics/MethodLength
328338

329-
def self.should_link_to_github(file)
330-
return unless file
331-
332-
file = file.realpath.to_path
333-
source_directory = Config.instance.source_directory.to_path
334-
file.start_with?(source_directory)
335-
end
336-
337-
# Construct Github token URL
338-
# @param [Hash] item Parsed doc child item
339-
# @param [Config] options Build options
340-
def self.gh_token_url(item, source_module)
341-
return unless github_prefix = source_module.github_file_prefix
342-
return unless should_link_to_github(item.file)
343-
344-
gh_line = if item.start_line && (item.start_line != item.end_line)
345-
"#L#{item.start_line}-L#{item.end_line}"
346-
else
347-
"#L#{item.line}"
348-
end
349-
relative_file_path = item.file.realpath.relative_path_from(
350-
source_module.root_path,
351-
)
352-
"#{github_prefix}/#{relative_file_path}#{gh_line}"
353-
end
354-
355339
# Build mustache item for a top-level doc
356340
# @param [Hash] item Parsed doc child item
357341
# @param [Config] options Build options
358342
# rubocop:disable Metrics/MethodLength
359343
def self.render_item(item, source_module)
360344
# Combine abstract and discussion into abstract
361345
abstract = (item.abstract || '') + (item.discussion || '')
346+
source_host_item_url = source_module.host&.item_url(item)
362347
{
363348
name: item.name,
364349
name_html: item.name.gsub(':', ':<wbr>'),
@@ -368,7 +353,8 @@ def self.render_item(item, source_module)
368353
other_language_declaration: item.display_other_language_declaration,
369354
usr: item.usr,
370355
dash_type: item.type.dash_type,
371-
github_token_url: gh_token_url(item, source_module),
356+
source_host_item_url: source_host_item_url,
357+
github_token_url: source_host_item_url,
372358
default_impl_abstract: item.default_impl_abstract,
373359
from_protocol_extension: item.from_protocol_extension,
374360
return: item.return,
@@ -452,8 +438,14 @@ def self.document(source_module, doc_model, path_to_root)
452438
doc[:tasks] = render_tasks(source_module, doc_model.children)
453439
doc[:module_name] = source_module.name
454440
doc[:author_name] = source_module.author_name
455-
doc[:github_url] = source_module.github_url
456-
doc[:github_token_url] = gh_token_url(doc_model, source_module)
441+
if source_host = source_module.host
442+
doc[:source_host_name] = source_host.name
443+
doc[:source_host_url] = source_host.url
444+
doc[:source_host_image] = source_host.image
445+
doc[:source_host_item_url] = source_host.item_url(doc_model)
446+
doc[:github_url] = doc[:source_host_url]
447+
doc[:github_token_url] = doc[:source_host_item_url]
448+
end
457449
doc[:dash_url] = source_module.dash_url
458450
doc[:path_to_root] = path_to_root
459451
doc[:deprecation_message] = doc_model.deprecation_message
Lines changed: 11 additions & 0 deletions
Loading
Lines changed: 23 additions & 0 deletions
Loading

lib/jazzy/podspec_documenter.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ def self.apply_config_defaults(podspec, config)
6464
config.version = podspec.version.to_s
6565
config.version_configured = true
6666
end
67-
unless config.github_file_prefix_configured
68-
config.github_file_prefix = github_file_prefix(podspec)
69-
config.github_file_prefix_configured = true
67+
unless config.source_host_files_url_configured
68+
config.source_host_files_url = github_file_prefix(podspec)
69+
config.source_host_files_url_configured = true
7070
end
7171
unless config.swift_version_configured
7272
trunk_swift_build = podspec.attributes_hash['pushed_with_swift_version']

0 commit comments

Comments
 (0)