sketchucation logo sketchucation
    • Login
    πŸ€‘ SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

    .gsub( /\n/, "\n\r" ) not working in WebDialog

    Scheduled Pinned Locked Moved Developers' Forum
    10 Posts 4 Posters 1.3k Views 4 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • onidarbeO Offline
      onidarbe
      last edited by onidarbe

      Strange thing I don't grasp:

      htmlCode = %{ <html> test </html>} win = UI::WebDialog.new( "" ) win.set_html( htmlCode.gsub(/\n/,"\n\r") ) win.show p htmlCode[7]

      When I right-click the WebDialog on Windows 7 selecting "View source", notepad opens and shows no new-lines or carriage-returns, so everything is on 1 line like this:
      <html> test</html>
      Although selecting this and copy/paste it here or in notepad++ does show new-lines and carriage-returns like:
      %(#0000FF)[<html>
      test
      </html>]

      It is solved when I add a \n after each line in %{...}
      although the Ruby Console is showing a 10, telling me there is alreeady a newline, \n
      So why doesn't .gsub(/\n/,"\n\r") work?!

      Another test shows .gsub(/\n/,"\n\r") is working, only not in the WebDialog !!!
      "<html>\ntest</html>".gsub(/\n/,"\n\r")[7] 13
      while this still shows 1 line in Notepad with "View source"
      win = UI::WebDialog.new( "" ) win.set_html( "<html>\ntest</html>".gsub(/\n/,"\n\r") ) win.show

      1 Reply Last reply Reply Quote 0
      • Dan RathbunD Offline
        Dan Rathbun
        last edited by

        gsub creates a NEW string. It does not change the source string.

        use gsub! instead, like:

        htmlCode = %{
        <html>
        test
        </html>}
        win = UI;;WebDialog.new( "" )
        htmlCode.gsub!(/\n/,"\n\r")
        win.set_html( htmlCode )
        win.show
        p htmlCode[7]
        

        htmlCode.gsub!(/\n/,"\n\r")
        is the similar to:
        htmlCode = htmlCode.gsub(/\n/,"\n\r")
        ... except that immediate methods (names ending with **!**,) return nil if no changes are made,... so do not use them in an assignment expression.

        πŸ’­

        I'm not here much anymore.

        1 Reply Last reply Reply Quote 0
        • Dan RathbunD Offline
          Dan Rathbun
          last edited by

          Another thing, in HTML use a %(#8000BF)[<BR>] tag to cause a linebreak, not "\n".

          I'm not here much anymore.

          1 Reply Last reply Reply Quote 0
          • tt_suT Offline
            tt_su
            last edited by

            @dan rathbun said:

            ... except that immediate methods (names ending with **!**,) return nil if no changes are made,... so do not use them in an assignment expression.

            This is such an annoying behaviour of Ruby. Source of so many creeping bugs.

            1 Reply Last reply Reply Quote 0
            • TIGT Offline
              TIG Moderator
              last edited by

              Enclosing <p> tags in html should recognize new lines in the html's text, and reflect that in what is displayed.
              Or as Dan says add <br> to force a new line in what is displayed...
              A \n will not work within html like you expected...

              Incidentally, do you have your rb files' encoding set to 'UTF-8 without BOM' ?
              You ought to...
              Notepad++ has easy options to re-encode existing ANSI files... and you can set the exe's options to make all new text files 'UTF-8 without BOM' anyway...

              TIG

              1 Reply Last reply Reply Quote 0
              • onidarbeO Offline
                onidarbe
                last edited by

                Thank you all.

                However gsub! isn't the solution, still shows everything on one line in notepad!
                I know <br> is the new-line in html, but I'd like to give out a html that also has new-lines using "View source" in notepad. Makes it more readable πŸ˜‰

                But I've found the problem so it is solved β˜€
                It's stupid but I should have used .gsub(/\n/,"**\r\n**") not .gsub(/\n/,"**\n\r**")

                1 Reply Last reply Reply Quote 0
                • tt_suT Offline
                  tt_su
                  last edited by

                  Yea, Notepad is fuzzy about new lines.

                  1 Reply Last reply Reply Quote 0
                  • onidarbeO Offline
                    onidarbe
                    last edited by

                    So this time it's not SU Ruby but MS-Windows πŸ˜‰

                    1 Reply Last reply Reply Quote 0
                    • Dan RathbunD Offline
                      Dan Rathbun
                      last edited by

                      @onidarbe said:

                      So this time it's not SU Ruby but MS-Windows πŸ˜‰

                      NotePad sucks.

                      To set View Source to Notepad++ run the reg file for your Windows bitsize:
                      (choose 32bit or 64bit Windows)

                      If you installed Notepad++ to a custom location, you will need to edit the regfile !

                      set_view_source_to_notepadpp.zip

                      I'm not here much anymore.

                      1 Reply Last reply Reply Quote 0
                      • onidarbeO Offline
                        onidarbe
                        last edited by

                        Thanks Dan!

                        But in this case I want for those maybe using my stuff to make it readable in notepad to πŸ˜‰

                        1 Reply Last reply Reply Quote 0
                        • 1 / 1
                        • First post
                          Last post
                        Buy SketchPlus
                        Buy SUbD
                        Buy WrapR
                        Buy eBook
                        Buy Modelur
                        Buy Vertex Tools
                        Buy SketchCuisine
                        Buy FormFonts

                        Advertisement