From dd31b0eb9cc8680ff2b67085a46ec8c33867881a Mon Sep 17 00:00:00 2001 From: Pixelastic Date: Fri, 1 Jul 2016 17:50:40 +0200 Subject: [PATCH] WIP: Need to make it work for 3.1.6 and without deprecation message --- Appraisals | 2 +- gemfiles/jekyll_v3.gemfile | 2 +- lib/record_extractor.rb | 22 ++++++---- .../collection-item.html | 0 .../collection-item.md | 0 spec/fixtures/jekyll_version_3/_config.yml | 2 +- spec/fixtures/jekyll_version_3/entreprise.md | 39 ++++++++++++++++++ spec/push_spec.rb | 1 + spec/record_extractor_spec.rb | 26 +++++++++--- spec/spec_helper.rb | 40 ++++++++++++++++--- 10 files changed, 112 insertions(+), 22 deletions(-) rename spec/fixtures/jekyll_version_3/{_my-collection => _api}/collection-item.html (100%) rename spec/fixtures/jekyll_version_3/{_my-collection => _api}/collection-item.md (100%) create mode 100644 spec/fixtures/jekyll_version_3/entreprise.md diff --git a/Appraisals b/Appraisals index efcc820..b731c10 100644 --- a/Appraisals +++ b/Appraisals @@ -3,6 +3,6 @@ appraise 'jekyll-v2' do end appraise 'jekyll-v3' do - gem 'jekyll', '~> 3.0' + gem 'jekyll', '3.1.6' gem 'jekyll-paginate', '~> 1.1.0' end diff --git a/gemfiles/jekyll_v3.gemfile b/gemfiles/jekyll_v3.gemfile index 080ad04..a72dc48 100644 --- a/gemfiles/jekyll_v3.gemfile +++ b/gemfiles/jekyll_v3.gemfile @@ -9,7 +9,7 @@ gem "json", "~> 1.8" gem "nokogiri", "~> 1.6" gem "html-hierarchy-extractor", :path => "../../html-hierarchy-extractor/" gem "verbal_expressions", "~> 0.1.5" -gem "jekyll", "~> 3.0" +gem "jekyll", "3.1.6" gem "jekyll-paginate", "~> 1.1.0" group :development do diff --git a/lib/record_extractor.rb b/lib/record_extractor.rb index a0cf3d2..c515765 100644 --- a/lib/record_extractor.rb +++ b/lib/record_extractor.rb @@ -67,12 +67,12 @@ class AlgoliaSearchRecordExtractor extname = File.extname(basename) slug = File.basename(basename, extname) - # Jekyll v2 posts have a specific slug method - return @file.slug if @file.respond_to? :slug - # Jekyll v3 posts have it in data return @file.data['slug'] if @file.data.key?('slug') + # Jekyll v2 posts have a specific slug method + return @file.slug if @file.respond_to?(:slug) + slug end @@ -81,11 +81,19 @@ class AlgoliaSearchRecordExtractor def tags tags = [] - # Jekyll v2 posts have a specific tags methods - tags = @file.tags if @file.respond_to?(:tags) + is_v2 = AlgoliaSearchUtils.restrict_jekyll_version(less_than: '3.0') + is_v3 = AlgoliaSearchUtils.restrict_jekyll_version(more_than: '3.0') + has_tags_method = @file.respond_to?(:tags) + has_tags_data = @file.data.key?('tags') - # Others have it in data - tags = @file.data['tags'] if tags.empty? && @file.data.key?('tags') + # Starting from Jekyll v3, all tags are in data['tags'] + tags = @file.data['tags'] if is_v3 && has_tags_data + + # In Jekyll v2, tags are in data['tags'], or in .tags + if is_v2 + tags = @file.tags if has_tags_method + tags = @file.data['tags'] if tags.empty? && has_tags_data + end # Some extension extends the tags with custom classes, so we make sure we # cast them as strings diff --git a/spec/fixtures/jekyll_version_3/_my-collection/collection-item.html b/spec/fixtures/jekyll_version_3/_api/collection-item.html similarity index 100% rename from spec/fixtures/jekyll_version_3/_my-collection/collection-item.html rename to spec/fixtures/jekyll_version_3/_api/collection-item.html diff --git a/spec/fixtures/jekyll_version_3/_my-collection/collection-item.md b/spec/fixtures/jekyll_version_3/_api/collection-item.md similarity index 100% rename from spec/fixtures/jekyll_version_3/_my-collection/collection-item.md rename to spec/fixtures/jekyll_version_3/_api/collection-item.md diff --git a/spec/fixtures/jekyll_version_3/_config.yml b/spec/fixtures/jekyll_version_3/_config.yml index d742190..b521ab8 100644 --- a/spec/fixtures/jekyll_version_3/_config.yml +++ b/spec/fixtures/jekyll_version_3/_config.yml @@ -1,5 +1,5 @@ collections: - my-collection: + api: output: true markdown_ext: 'md,mkd' paginate: 1 diff --git a/spec/fixtures/jekyll_version_3/entreprise.md b/spec/fixtures/jekyll_version_3/entreprise.md new file mode 100644 index 0000000..bf09db0 --- /dev/null +++ b/spec/fixtures/jekyll_version_3/entreprise.md @@ -0,0 +1,39 @@ +--- +title: API Entreprise +tagline: Simplifier les démarches des entreprises en récupérant pour elles leurs documents administratifs +doc_tech: https://api.apientreprise.fr/docs +domain: http://api.apientreprise.fr +contract: OUVERT sous contrat +stat: + lastXdays: 30 + url: https://dashboard.apientreprise.fr/api/stats/apientreprise/last_30_days_requests + label: informations non redemandées aux entreprises +clients: + - Collectivités + - Administrations + - Ministères + - Places de marchés +partners: + - DGFiP + - ACOSS + - CNETP + - INSEE + - INFOGREFFE + - FNTP + - MSA + - OPQIBI + - PRO BTP + - QUALIBAT + - MIN INTERIEUR +owner: DINSIC +category: confidential +keywords: + - etablissement + - entreprise + - certification +access_link: https://tps.apientreprise.fr/users/dossiers/new?procedure_id=33 +--- + +L’APIENTREPRISE est une plateforme d’échange opérée par le SGMAP qui met à disposition des opérateurs publics et des administrations, des données et des documents administratifs de référence, relatifs aux entreprises et association, qui sont délivrés par les administrations et les organismes publics, à fin de simplifier les démarches administratives et la gestion des dossiers. + +Composante de l’Etat plateforme, l’accès à l’APIENTREPRISE est modérée et régulée par le SGMAP, qui attribue à chaque client des autorisations de récupération d’informations selon la nature des démarches à traiter (Marchés publics simplifiés, aides publiques simplifiés…) diff --git a/spec/push_spec.rb b/spec/push_spec.rb index ee5e558..c775baf 100644 --- a/spec/push_spec.rb +++ b/spec/push_spec.rb @@ -54,6 +54,7 @@ describe(AlgoliaSearchJekyllPush) do end it 'keeps markdown documents' do + ap document_file expect(push.indexable?(document_file)).to eq true end diff --git a/spec/record_extractor_spec.rb b/spec/record_extractor_spec.rb index 57b5536..354255d 100644 --- a/spec/record_extractor_spec.rb +++ b/spec/record_extractor_spec.rb @@ -14,15 +14,10 @@ describe(AlgoliaSearchRecordExtractor) do let(:fixture_front_matter) do extractor.new(site.file_by_name('front_matter.md')) end - # let(:html_page_file) { extractor.new(site.file_by_name('authors.html')) } - # let(:hierarchy_page_file) { - # extractor.new(site.file_by_name('hierarchy.md')) - # } - # let(:weight_page_file) { extractor.new(site.file_by_name('weight.md')) } before(:each) do # Disabling the logs, while still allowing to spy them - Jekyll.logger = double('Specific Mock Logger').as_null_object + # Jekyll.logger = double('Specific Mock Logger').as_null_object @logger = Jekyll.logger.writer end @@ -152,6 +147,25 @@ describe(AlgoliaSearchRecordExtractor) do expect(actual).to eq 'collection-item' end + + # if restrict_jekyll_version(more_than: '3.0') + # fit 'should not throw a deprecation warning' do + # # Given + # input = fixture_post + + # # When + # # allow(Jekyll).to receive(:logger) do + # # double('AAA').as_null_object + # # end + # # Jekyll.logger = double('BBB').as_null_object + # # Jekyll.logger.writer = double('CCC').as_null_object + + # actual = input.slug + + + # # expect(actual).to eq 'collection-item' + # end + # end end describe 'tags' do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 3b1e864..79ae0cb 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -34,16 +34,44 @@ def get_site(config = {}, options = {}) site = AlgoliaSearchJekyllPush.init_options({}, options, config) .jekyll_new(config) + + def site.file_by_name(file_name) - # We first check the exact match + # We build the list of all the files in the website. Starting from jekyll + # 1.3.4, collections are no longer returned by each_site_file. + files = {} + + ap self + # Fails sur 1.3.6, marche sur 3.1.3 + # Pour le moment on va rester à le faire marcher sur 3.1.3 + # On va poser les tests pour s'assurer que jekyll 3 ne lance pas de warning + # de deprecation + # Une fois ok, on vérifie que api.gouv.fr fonctionne avec 1.3.6 + # Si oui, on regarde comment ils passent les collections à la vue et on fait + # pareil pour chopper les documents + # Si non, on file un bug + + # We get the list of all classic files each_site_file do |file| - return file if file.path == file_name + files[file.path] = file + end + # Same for the collections + collections.each do |collection_name, items| + ap collection_name + items.each do |item| + files[item.path] = item + end end - # If not found (happens for static files that uses a full absolute path), we - # try with a loose regexp - each_site_file do |file| - return file if file.path =~ /#{file_name}$/ + # If we have an exact match, we use that one: + return files[file_name] if files.key?(file_name) + + # Otherwise we try to find a key that is loosely matching + keys = files.keys + values = files.values + + keys.each_with_index do |key, index| + return values[index] if key =~ /#{file_name}$/ end nil