Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use of a valid ruby extension but failure to generate the pdf file #1559

Closed
gemkod opened this issue Mar 6, 2024 · 3 comments
Closed

use of a valid ruby extension but failure to generate the pdf file #1559

gemkod opened this issue Mar 6, 2024 · 3 comments

Comments

@gemkod
Copy link

gemkod commented Mar 6, 2024

Hello,

Observed vs. expected behavior

I'm trying to use the extension described here https://docs.asciidoctor.org/pdf-converter/latest/extend/use-cases/#theme-table-using-roles.

class PDFConverterTableRole < (Asciidoctor::Converter.for 'pdf')
  register_for 'pdf'

  def convert_table node
    if node.role ?
      key_prefix = %(role_<table>_#{node.roles[0]}_)
      unless (role_entries = theme.each_pair.select {|name, val| name.to_s.start_with ? key_prefix }).empty ?
        save_theme do
          role_entries.each do |name, value|
            theme[%(table_#{name.to_s.delete_key_prefix})] = val
          end
          super
        end
        return
      end
    end
    super
  end
end

I placed the extension in .asciidocotor/lib/TableRole.rb at the root of the project.

When I preview it, I get the following error:

Unable to render AsciiDoc document

org.jruby.exceptions.TypeError : (TypeError) superclass must be a Class (NilClass given)

I tried adding the following line to the beginning of the ruby file:

require 'asciidoctor/pdf

In this case the preview works but generating the pdf fails with the plugin button.

I manage to get the preview and pdf generation to work without using the "require" line and by writing the following inheritance:
PDFConverterTableRole class < Asciidoctor::Converter::Base.
Until yesterday (version 2023.2.3 Phpstorm), this worked. Since the update to 2023.2.5, it no longer works.

Environment

Asciidoc plugin version: 0.40.9

IDE details : PhpStorm 2023.2.5
Build #PS-232.10300.41, built on 14 February 2024
Under license ***
Subscription is active until 20 November 2024.
Runtime version: 17.0.10+7-b1000.48 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
Windows 10.0
GC: G1 Young Generation, G1 Old Generation
Memory: 4082M
Cores: 20
Registry :
ide.intellij.laf.enable.animation=true
debugger.new.tool.window.layout=true
run.processes.with.pty=TRUE
ide.experimental.ui=true
ide.balloon.shadow.size=0

Ungrouped plugins :
com.mallowigi.idea (22.0.0)
com.intellij.ideolog (222.3.2.0)
com.markskelton.one-dark-theme (5.10.0)
com.intellij.properties (232.8660.88)
String Manipulation (9.12.0)
Key Promoter X (2023.3.0)
com.github.gitofleonardo.simplesqlitebrowser (1.0.7)
com.intellij.plugin.adernov.powershell (2.3.1)
PlantUML integration (7.7.0-IJ2023.2)
org.asciidoctor.intellij.asciidoc (0.40.9)
com.jetbrains.lang.ejs (232.8660.142)
mobi.hsz.idea.gitignore (4.5.2)
com.intellij.plugins.html.instantEditing (232.8660.142)
izhangzhihao.rainbow.brackets (2023.3.7)
com.kalessil.phpStorm.phpInspectionsEA (5.0.0.0)
com.github.ArtsiomCh.NestedBracketsColorer (0.

@gemkod gemkod added the bug label Mar 6, 2024
@ahus1 ahus1 added question and removed bug labels May 26, 2024
@ahus1 ahus1 self-assigned this May 26, 2024
@ahus1
Copy link
Contributor

ahus1 commented May 26, 2024

Given my limited understanding of Ruby, the extension you're adding expects that the PDF support for AsciiDoc is loaded, and it will fail if that PDF support is not available. The behavior might have changed with an upgrade of the AsciiDoc plugin which might have happened automatically when you upgraded PhpStorm.

The AsciIDoc plugin for IntelliJ will only load the PDF support when you ask it to render a PDF, and will not load the PDF support when it renders the regular preview.

With some trial-and-error, I updated the code to use a replacement for the non-existing PDF converter when generating the preview. Now the preview renders normally. There might be better ways to do this, maybe a Ruby expert can comment here.

The generating of a PDF works as well. As I'm lacking a full example, I wasn't able to generate your PDF with the specific setup you are using, so I don't know if the output still looks like you expect.

Please let me know if this is helpful.

class PDFConverterTableRole < ((Asciidoctor::Converter.for 'pdf') || (Asciidoctor::Converter.for 'html5'))
  register_for 'pdf'

  def convert_table node
    if node.role ?
      key_prefix = %(role_<table>_#{node.roles[0]}_)
      unless (role_entries = theme.each_pair.select {|name, val| name.to_s.start_with ? key_prefix }).empty ?
        save_theme do
          role_entries.each do |name, value|
            theme[%(table_#{name.to_s.delete_key_prefix})] = val
          end
          super
        end
        return
      end
    end
    super
  end
end

@mojavelinux
Copy link
Member

class PDFConverterTableRole < ((Asciidoctor::Converter.for 'pdf') || (Asciidoctor::Converter.for 'html5'))

You would never want to register the PDF converter by extending the HTML converter. These two converters are not interchangeable. However, you could wrap the class definition in if Asciidoctor::Converter.for 'pdf' so it is only used if the PDF converter is available.

@ahus1
Copy link
Contributor

ahus1 commented Jun 6, 2024

Thank you - this is obviously better. See below for the complete source code snippet for future reference. I'm closing this issue as the question has been answerd.

if Asciidoctor::Converter.for 'pdf'
  class PDFConverterTableRole < (Asciidoctor::Converter.for 'pdf')
    register_for 'pdf'

    def convert_table node
      if node.role?
        key_prefix = %(role_<table>_#{node.roles[0]}_)
        unless (role_entries = theme.each_pair.select {|name, val| name.to_s.start_with? key_prefix }).empty?
          save_theme do
            role_entries.each do |name, val|
              theme[%(table_#{name.to_s.delete_prefix key_prefix})] = val
            end
            super
          end
          return
        end
      end
      super
    end
  end
end

@ahus1 ahus1 closed this as completed Jun 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants